noObjectConstructor (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Rule available since:
v2.6.0 - Diagnostic Category:
lint/nursery/noObjectConstructor - This rule has an unsafe fix.
- The default severity of this rule is information.
- Sources:
- Same as
no-object-constructor - Inspired from
no-new-object
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noObjectConstructor": "error" } } }}Description
Section titled “Description”Disallow calling Object without arguments to create an empty object.
Object() and new Object() without arguments both create an empty object, the same as {}.
Writing {} is shorter and clearer, and it always creates a plain object,
even if other code replaces the global Object.
Calls with an argument, such as Object(value), are allowed because they convert value to an object.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”const obj = Object();code-block.js:1:13 lint/nursery/noObjectConstructor FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Calling Object without arguments creates an empty object.
> 1 │ const obj = Object();
│ ^^^^^^^^
2 │
ℹ Writing {} is shorter, and it always creates a plain object, even if other code replaces the global Object.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use an object literal.
1 │ - const·obj·=·Object();
1 │ + const·obj·=·{};
2 2 │
const obj = new Object();code-block.js:1:13 lint/nursery/noObjectConstructor FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Calling Object without arguments creates an empty object.
> 1 │ const obj = new Object();
│ ^^^^^^^^^^^^
2 │
ℹ Writing {} is shorter, and it always creates a plain object, even if other code replaces the global Object.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use an object literal.
1 │ - const·obj·=·new·Object();
1 │ + const·obj·=·{};
2 2 │
const obj = new Object;code-block.js:1:13 lint/nursery/noObjectConstructor FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Calling Object without arguments creates an empty object.
> 1 │ const obj = new Object;
│ ^^^^^^^^^^
2 │
ℹ Writing {} is shorter, and it always creates a plain object, even if other code replaces the global Object.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use an object literal.
1 │ - const·obj·=·new·Object;
1 │ + const·obj·=·{};
2 2 │
const obj = {};const wrapped = Object(value);const alsoWrapped = new Object(value);// Here, `Object` is a parameter, not the built-in `Object`.const createObject = (Object) => new Object();See Also
Section titled “See Also”- To use
[]instead of theArrayconstructor, seeuseArrayLiterals. - To require
newwhen calling built-in constructors such asObject, seeuseConsistentBuiltinInstantiation. This rule reports bothObject()andnew Object()when they have no arguments.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.