Skip to content

noInvalidNewBuiltin

诊断类别:lint/correctness/noInvalidNewBuiltin

¥Diagnostic Category: lint/correctness/noInvalidNewBuiltin

自从:v1.3.0

¥Since: v1.3.0

禁止具有全局非构造函数的 new 运算符。

¥Disallow new operators with global non-constructor functions.

某些全局函数无法使用 new 运算符调用,如果你尝试这样做,则会抛出 TypeError。这些函数是:

¥Some global functions cannot be called using the new operator and will throw a TypeError if you attempt to do so. These functions are:

¥Examples

¥Invalid

let foo = new Symbol('foo');
code-block.js:1:11 lint/correctness/noInvalidNewBuiltin  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Symbol cannot be called as a constructor.

> 1 │ let foo = new Symbol(‘foo’);
^^^^^^^^^^^^^^^^^
2 │

Calling Symbol with the new operator throws a TypeError.

Unsafe fix: Remove new.

1 │ let·foo·=·new·Symbol(‘foo’);
----
let bar = new BigInt(9007199254740991);
code-block.js:1:11 lint/correctness/noInvalidNewBuiltin  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

BigInt cannot be called as a constructor.

> 1 │ let bar = new BigInt(9007199254740991);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

Calling BigInt with the new operator throws a TypeError.

Unsafe fix: Remove new.

1 │ let·bar·=·new·BigInt(9007199254740991);
----

¥Valid

let foo = Symbol('foo');
function baz(Symbol) {
const qux = new Symbol("baz");
}
let bar = BigInt(9007199254740991);
function quux(BigInt) {
const corge = new BigInt(9007199254740991);
}

¥Related links