Skip to content

noGlobalObjectCalls

诊断类别:lint/correctness/noGlobalObjectCalls

¥Diagnostic Category: lint/correctness/noGlobalObjectCalls

自从:v1.0.0

¥Since: v1.0.0

来源:

¥Sources:

禁止将全局对象属性作为函数调用

¥Disallow calling global object properties as functions

ECMAScript 提供了几个旨在按原样使用的全局对象。其中一些对象由于其大写(例如 Math 和 JSON)看起来像是构造函数,但如果你尝试将它们作为函数执行,则会抛出错误。

¥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 5 规范明确指出无法调用 Math 和 JSON:Math 对象没有 [[Call]] 内部属性;无法将 Math 对象作为函数调用。

¥The ECMAScript 5 specification makes it clear that both Math and JSON cannot be invoked: The Math object does not have a [[Call]] internal property; it is not possible to invoke the Math object as a function.

ECMAScript 2015 规范明确指出无法调用 Reflect:Reflect 对象也没有 [[Call]] 内部方法;无法将 Reflect 对象作为函数调用。

¥The ECMAScript 2015 specification makes it clear that Reflect cannot be invoked: The Reflect object also does not have a [[Call]] internal method; it is not possible to invoke the Reflect object as a function.

ECMAScript 2017 规范明确指出无法调用 Atomics:Atomics 对象没有 [[Call]] 内部方法;无法将 Atomics 对象作为函数调用。

¥The ECMAScript 2017 specification makes it clear that Atomics cannot be invoked: The Atomics object does not have a [[Call]] internal method; it is not possible to invoke the Atomics object as a function.

ECMAScript 国际化 API 规范明确指出无法调用 Intl:Intl 对象没有 [[Call]] 内部方法;无法将 Intl 对象作为函数调用。

¥And the ECMAScript Internationalization API Specification makes it clear that Intl cannot be invoked: The Intl object does not have a [[Call]] internal method; it is not possible to invoke the Intl object as a function.

¥Examples

¥Invalid

var math = Math();
code-block.js:1:12 lint/correctness/noGlobalObjectCalls ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Math is not a function.

> 1 │ var math = Math();
^^^^
2 │

var newMath = new Math();
code-block.js:1:19 lint/correctness/noGlobalObjectCalls ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Math is not a function.

> 1 │ var newMath = new Math();
^^^^
2 │

var json = JSON();
code-block.js:1:12 lint/correctness/noGlobalObjectCalls ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Json is not a function.

> 1 │ var json = JSON();
^^^^
2 │

var newJSON = new JSON();
code-block.js:1:19 lint/correctness/noGlobalObjectCalls ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Json is not a function.

> 1 │ var newJSON = new JSON();
^^^^
2 │

var reflect = Reflect();
code-block.js:1:15 lint/correctness/noGlobalObjectCalls ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Reflect is not a function.

> 1 │ var reflect = Reflect();
^^^^^^^
2 │

var newReflect = new Reflect();
code-block.js:1:22 lint/correctness/noGlobalObjectCalls ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Reflect is not a function.

> 1 │ var newReflect = new Reflect();
^^^^^^^
2 │

var atomics = Atomics();
code-block.js:1:15 lint/correctness/noGlobalObjectCalls ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Atomics is not a function.

> 1 │ var atomics = Atomics();
^^^^^^^
2 │

var newAtomics = new Atomics();
code-block.js:1:22 lint/correctness/noGlobalObjectCalls ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Atomics is not a function.

> 1 │ var newAtomics = new Atomics();
^^^^^^^
2 │

var intl = Intl();
code-block.js:1:12 lint/correctness/noGlobalObjectCalls ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Intl is not a function.

> 1 │ var intl = Intl();
^^^^
2 │

var newIntl = new Intl();
code-block.js:1:19 lint/correctness/noGlobalObjectCalls ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Intl is not a function.

> 1 │ var newIntl = new Intl();
^^^^
2 │

¥Valid

function area(r) {
return Math.PI * r * r;
}
var object = JSON.parse("{}");
var value = Reflect.get({ x: 1, y: 2 }, "x");
var first = Atomics.load(foo, 0);
var segmenterFr = new Intl.Segmenter("fr", { granularity: "word" });

¥Related links