noGlobalObjectCalls
Summary
Section titled “Summary”- Rule available since:
v1.0.0 - Diagnostic Category:
lint/correctness/noGlobalObjectCalls - This rule is recommended, meaning it is enabled by default.
- This rule doesn’t have a fix.
- The default severity of this rule is error.
- Sources:
- Same as
no-obj-calls
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "correctness": { "noGlobalObjectCalls": "error" } } }}Description
Section titled “Description”Disallow calling global object properties as functions
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.
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.
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.
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.
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
Section titled “Examples”Invalid
Section titled “Invalid”var math = Math();code-block.js:1:12 lint/correctness/noGlobalObjectCalls ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Math cannot be called as a function.
> 1 │ var math = Math();
│ ^^^^
2 │
ℹ Math is a global object with static members, not a callable constructor or function.
ℹ Use one of its properties or methods instead of calling the object itself.
var newMath = new Math();code-block.js:1:19 lint/correctness/noGlobalObjectCalls ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Math cannot be called as a function.
> 1 │ var newMath = new Math();
│ ^^^^
2 │
ℹ Math is a global object with static members, not a callable constructor or function.
ℹ Use one of its properties or methods instead of calling the object itself.
var json = JSON();code-block.js:1:12 lint/correctness/noGlobalObjectCalls ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ JSON cannot be called as a function.
> 1 │ var json = JSON();
│ ^^^^
2 │
ℹ JSON is a global object with static members, not a callable constructor or function.
ℹ Use one of its properties or methods instead of calling the object itself.
var newJSON = new JSON();code-block.js:1:19 lint/correctness/noGlobalObjectCalls ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ JSON cannot be called as a function.
> 1 │ var newJSON = new JSON();
│ ^^^^
2 │
ℹ JSON is a global object with static members, not a callable constructor or function.
ℹ Use one of its properties or methods instead of calling the object itself.
var reflect = Reflect();code-block.js:1:15 lint/correctness/noGlobalObjectCalls ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Reflect cannot be called as a function.
> 1 │ var reflect = Reflect();
│ ^^^^^^^
2 │
ℹ Reflect is a global object with static members, not a callable constructor or function.
ℹ Use one of its properties or methods instead of calling the object itself.
var newReflect = new Reflect();code-block.js:1:22 lint/correctness/noGlobalObjectCalls ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Reflect cannot be called as a function.
> 1 │ var newReflect = new Reflect();
│ ^^^^^^^
2 │
ℹ Reflect is a global object with static members, not a callable constructor or function.
ℹ Use one of its properties or methods instead of calling the object itself.
var atomics = Atomics();code-block.js:1:15 lint/correctness/noGlobalObjectCalls ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Atomics cannot be called as a function.
> 1 │ var atomics = Atomics();
│ ^^^^^^^
2 │
ℹ Atomics is a global object with static members, not a callable constructor or function.
ℹ Use one of its properties or methods instead of calling the object itself.
var newAtomics = new Atomics();code-block.js:1:22 lint/correctness/noGlobalObjectCalls ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Atomics cannot be called as a function.
> 1 │ var newAtomics = new Atomics();
│ ^^^^^^^
2 │
ℹ Atomics is a global object with static members, not a callable constructor or function.
ℹ Use one of its properties or methods instead of calling the object itself.
var intl = Intl();code-block.js:1:12 lint/correctness/noGlobalObjectCalls ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Intl cannot be called as a function.
> 1 │ var intl = Intl();
│ ^^^^
2 │
ℹ Intl is a global object with static members, not a callable constructor or function.
ℹ Use one of its properties or methods instead of calling the object itself.
var newIntl = new Intl();code-block.js:1:19 lint/correctness/noGlobalObjectCalls ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Intl cannot be called as a function.
> 1 │ var newIntl = new Intl();
│ ^^^^
2 │
ℹ Intl is a global object with static members, not a callable constructor or function.
ℹ Use one of its properties or methods instead of calling the object itself.
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
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.