Skip to content

noNewSymbol

诊断类别:lint/correctness/noNewSymbol

¥Diagnostic Category: lint/correctness/noNewSymbol

自从:v1.0.0

¥Since: v1.0.0

来源:

¥Sources:

禁止将 new 运算符与 Symbol 对象一起使用。

¥Disallow new operators with the Symbol object.

Symbol 无法实例化。这会导致抛出 TypeError

¥Symbol cannot be instantiated. This results in throwing a TypeError.

¥Examples

¥Invalid

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

Symbol cannot be called as a constructor.

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

Unsafe fix: Remove new.

1 │ var·foo·=·new·Symbol(‘foo’);
----

¥Valid

var bar = Symbol('bar');
function baz() {
function Symbol() { }
new Symbol();
}

¥Related links