Illegal invocation¶
ID: js/illegal-invocation
Kind: problem
Security severity:
Severity: error
Precision: high
Tags:
- correctness
- language-features
Query suites:
- javascript-security-and-quality.qls
Click to see the query in the CodeQL repository
Class methods and arrow functions must not be invoked using new
, and attempting to do so will result in a runtime error.
Conversely, constructors can only be invoked using new
or super(...)
, and attempting to invoke them as a normal function will result in a runtime error.
Recommendation¶
Correct the invocation in question by adding or removing new
as appropriate.
Example¶
In the following example, Point
is a class, but on line 8 it is invoked without new
. This will lead to a runtime error.
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
let p = Point(23, 42);
Instead, new
should be used:
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
let p = new Point(23, 42);
References¶
Mozilla Developer Network: Constructors.
Mozilla Developer Network: Arrow functions.
Mozilla Developer Network: Method definitions.