我是Node.js的新手,我有一些问题。我在sonarqube中收到了一个错误,因为它定义了一个常量,而不是复制5次"-deleteFlag“。我怎样才能解决这个问题。
export class CCGuid {
"-deleteFlag": string;
"#text": string;
constructor(obj: any) {
if (typeof obj === "string") {
this["#text"] = obj;
this["-deleteFlag"] = "N";
} else {
try {
this["-deleteFlag"] = obj["-deleteFlag"];
} catch {
this["-deleteFlag"] = undefined;
}
try {
this["#text"] = obj["#text"];
} catch {
this["#text"] = undefined;
}
}
}
}发布于 2022-07-07 10:06:49
export class CCGuid {
"-deleteFlag": string;
"#text": string;
constructor(obj: any) {
const deleteFlag = "-deleteFlag";
if (typeof obj === "string") {
this["#text"] = obj;
this[deleteFlag] = "N";
} else {
try {
this[deleteFlag] = obj[deleteFlag];
} catch {
this[deleteFlag] = undefined;
}
try {
this["#text"] = obj["#text"];
} catch {
this["#text"] = undefined;
}
}
}
} 我认为这应该适用于SQ,至少当涉及到特定的变量时。当然,您也可以对"#text"进行同样的操作。
编辑后:对不起,我的第一个答案被打断了,我很匆忙,没有意识到我在写什么。
考虑到我的更新片段,您可以执行以下操作:
this[deleteFlag']:这会有用的。
this['-deleteFlag']:当然可以,但是Sonar会抱怨,因为你使用的是重复的字符串文字。
this.deleteFlag:这不起作用,因为它会在对象上寻找一个deleteFlag键。这样的密钥不存在,它是'-deleteFlag'。
this['deleteFlag']:这在功能上与上面的行相同。将在对象上查找一个不存在的'deletFlag'密钥。
抱歉弄乱了!希望这能帮上忙
https://stackoverflow.com/questions/72895856
复制相似问题