自我赋值¶
ID: js/redundant-assignment
Kind: problem
Security severity:
Severity: warning
Precision: high
Tags:
- reliability
- correctness
- external/cwe/cwe-480
- external/cwe/cwe-561
Query suites:
- javascript-security-and-quality.qls
将变量赋值给自己通常表示错误,例如缺少 this
限定符或变量名称拼写错误。
建议¶
仔细检查赋值,查看是否有拼写错误或缺少限定符。
如果自我赋值是故意的,并且是出于文档或优化目的,请添加包含 @type
标签的 JSDoc 注释。这将表明自我赋值是故意的。
示例¶
在下面的示例中,构造函数 Rectangle
旨在将属性 x
、y
、width
和 height
初始化为同名参数的值。
function Rectangle(x, y, width, height) {
this.x = x;
this.y = y;
width = width;
this.height = height;
}
但是,请注意,在第 4 行中,程序员忘记用 this
限定赋值的左侧:代码现在执行了将 width
参数赋值给自身的无用操作,并且 width
属性保持未初始化状态。
要解决此问题,请插入 this
限定符
function Rectangle(x, y, width, height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}