Versions

no-new-symbol

Disallow new operators with the Symbol object

Recommended

The "extends": "eslint:recommended" property in a configuration file enables this rule

Symbol 不打算与 new 运算符一起使用,而是作为一个函数来调用。

var foo = new Symbol("foo");

这将抛出一个 TypeError 异常。

规则细节

这条规则的目的是防止用 new 运算符意外地调用 Symbol

示例

使用此规则的错误示例:

Open in Playground
/*eslint no-new-symbol: "error"*/
/*eslint-env es6*/

var foo = new Symbol('foo');

使用此规则的正确示例:

Open in Playground
/*eslint no-new-symbol: "error"*/
/*eslint-env es6*/

var foo = Symbol('foo');

// Ignores shadowed Symbol.
function bar(Symbol) {
    const baz = new Symbol("baz");
}

何时不用

不应该在 ES3/5 环境中使用此规则。

Handled by TypeScript

It is safe to disable this rule when using TypeScript because TypeScript's compiler enforces this check.

Version

This rule was introduced in ESLint v2.0.0-beta.1.

Further Reading

Resources

更改语言