disallow calling global object properties as functions (no-obj-calls)

禁止将全局对象当作函数进行调用 (no-obj-calls)

ECMAScript provides several global objects that are intended to be used as-is. Some of these objects look as if they could be constructors due their capitalization (such as Math and JSON) but will throw an error if you try to execute them as functions.

ECMAScript 提供了几个全局对象,旨在直接调用。这些对象由于是大写的(比如 MathJSON)看起来像是构造函数,但是如果你尝试像函数一样执行它们,将会抛出错误。

The ECMAScript 5 specification makes it clear that both Math and JSON cannot be invoked:

ECMAScript 5 规范明确表示 MathJSON 是不能被调用的:

The Math object does not have a [[Call]] internal property; it is not possible to invoke the Math object as a function.

Math 对象没有 [[Call]] 内部属性,不能像一个函数一样调用 Math 对象

And the ECMAScript 2015 specification makes it clear that Reflect cannot be invoked:

ECMAScript 2015 specification 明确表明 Reflect 不能被调用:

The Reflect object also does not have a [[Call]] internal method; it is not possible to invoke the Reflect object as a function.

Reflect 对象没有 [[Call]] 内置方法;无法像调用函数一样调用 Reflect 对象。

Rule Details

This rule disallows calling the Math, JSON and Reflect objects as functions.

该规则禁止将 MathJSONReflect 对象当作函数进行调用。

Examples of incorrect code for this rule:

错误 代码示例:

/*eslint no-obj-calls: "error"*/

var math = Math();
var json = JSON();
var reflect = Reflect();

Examples of correct code for this rule:

正确 代码示例:

/*eslint no-obj-calls: "error"*/

function area(r) {
    return Math.PI * r * r;
}
var object = JSON.parse("{}");
var value = Reflect.get({ x: 1, y: 2 }, "x");

Further Reading

Version

This rule was introduced in ESLint 0.0.9.

该规则在 ESLint 0.0.9 中被引入。

Resources


+