非法调用¶
ID: js/illegal-invocation
Kind: problem
Security severity:
Severity: error
Precision: high
Tags:
- correctness
- language-features
Query suites:
- javascript-security-and-quality.qls
类方法和箭头函数不应使用 new
调用,尝试这样做会导致运行时错误。
反之,构造函数只能使用 new
或 super(...)
调用,尝试将其作为普通函数调用会导致运行时错误。
建议¶
通过根据需要添加或删除 new
来修正有问题的调用。
示例¶
在以下示例中,Point
是一个类,但在第 8 行它是在没有 new
的情况下调用的。这会导致运行时错误。
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
let p = Point(23, 42);
相反,应该使用 new
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
let p = new Point(23, 42);