Pattern: Empty Object()
constructor call
Issue: -
Using the Object
constructor to create empty objects is discouraged in favor of object literal notation. The only valid use case for Object()
or new Object()
is when intentionally wrapping a value to create an object.
Example of incorrect code:
const obj = new Object();
const dict = Object();
function createObj() {
return new Object();
}
Example of correct code:
const obj = {};
const dict = Object.create(null);
// Wrapping values is ok
const str = Object("foo");
const num = Object(123);
function isObject(value) {
return value === Object(value);
}