The Object
object provides a variety of methods to work with them and their properties. Let’s look at some of the most useful ones.
Creation and Cloning
Method | Description |
---|---|
Object.create() | Creates a new object using an existing object as a prototype. |
Object.assign() | Copies all enumerable properties from one or more source objects to a target object. |
Object.fromEntries() | Creates a new object from a list of key-value pairs. |
Creates a new object with a specific prototype.
const proto = { greet: function() { return "Hello"; } };
const obj = Object.create(proto);
console.log(obj.greet()); // "Hello"
Copies all enumerable properties from one or more source objects to a target object.
const target = { a: 1 };
const source = { b: 2, c: 3 };
const result = Object.assign(target, source);
console.log(result); // { a: 1, b: 2, c: 3 }
Converts a list of key-value pairs into an object.
const entries = [['name', 'Alice'], ['age', 30]];
const obj = Object.fromEntries(entries);
console.log(obj); // { name: 'Alice', age: 30 }
Iteration over Objects
Method | Description |
---|---|
Object.keys() | Returns an array of property names |
Object.values() | Returns an array of property values |
Object.entries() | Returns an array of [key, value] pairs of the enumerable properties of an object. |
Returns an array of the object’s enumerable property names.
const obj = { a: 1, b: 2, c: 3 };
const keys = Object.keys(obj);
console.log(keys); // ["a", "b", "c"]
Returns an array of the object’s enumerable property values.
const obj = { a: 1, b: 2, c: 3 };
const values = Object.values(obj);
console.log(values); // [1, 2, 3]
Returns an array of [key, value] pairs of the object’s enumerable properties.
const obj = { a: 1, b: 2, c: 3 };
const entries = Object.entries(obj);
console.log(entries); // [["a", 1], ["b", 2], ["c", 3]]
Accessing Properties
Method | Description |
---|---|
Object.hasOwnProperty() | Returns a boolean indicating whether the object has the property as its own. |
Object.hasOwn() | Similar to hasOwnProperty but more modern. |
Object.getOwnPropertyNames() | Returns an array of all property names of an object. |
Object.getOwnPropertyDescriptor() | Returns the descriptor of a specific property of an object. |
Returns a boolean indicating whether the object has the specified property as its own (not inherited).
const obj = { a: 1 };
console.log(obj.hasOwnProperty('a')); // true
console.log(obj.hasOwnProperty('b')); // false
Returns an array of all property names (enumerable and non-enumerable) of an object.
const obj = { a: 1, b: 2, c: 3 };
const propNames = Object.getOwnPropertyNames(obj);
console.log(propNames); // ["a", "b", "c"]
Returns a property descriptor for a specific property of an object.
const obj = { a: 1 };
const descriptor = Object.getOwnPropertyDescriptor(obj, 'a');
console.log(descriptor);
// { value: 1, writable: true, enumerable: true, configurable: true }
Property Manipulation
Method | Description |
---|---|
Object.defineProperty() | Defines or modifies a property of an object |
Object.defineProperties() | Defines or modifies multiple properties of an object. |
Defines a new property or modifies an existing one in an object, controlling its configuration.
const obj = {};
Object.defineProperty(obj, 'name', { value: 'Alice', writable: false });
console.log(obj.name); // "Alice"
obj.name = 'Bob'; // Doesn't change because writable is false
console.log(obj.name); // "Alice"
Defines several new properties or modifies existing ones in an object, controlling their configurations.
const obj = {};
Object.defineProperties(obj, {
name: { value: 'Alice', writable: true },
age: { value: 30, writable: false }
});
console.log(obj.name); // "Alice"
console.log(obj.age); // 30
Prototypes
Method | Description |
---|---|
Object.getPrototypeOf() | Returns the prototype of a specified object. |
Object.setPrototypeOf() | Sets the prototype of a specified object to another object. |
Object.isPrototypeOf() | Returns a boolean indicating if an object is in the prototype chain of another object. |
Returns the prototype (i.e., the value of [[Prototype]]
) of the specified object.
const proto = {};
const obj = Object.create(proto);
const prototype = Object.getPrototypeOf(obj);
console.log(prototype === proto); // true
Sets the prototype (i.e., the value of [[Prototype]]
) of a specific object.
const proto = { greet: function() { return "Hello"; } };
const obj = {};
Object.setPrototypeOf(obj, proto);
console.log(obj.greet()); // "Hello"
Returns a boolean indicating if the specified object is in the prototype chain of the object.
const proto = {};
const obj = Object.create(proto);
console.log(proto.isPrototypeOf(obj)); // true
Comparison and Verification
Method | Description |
---|---|
Object.is() | Determines whether two values are considered the same value. |
Object.isExtensible() | Determines if an object can have new properties added to it. |
Object.isFrozen() | Determines if an object is frozen. |
Object.isSealed() | Determines if an object is sealed. |
Determines if two values are equal (more precise than ===
for NaN and +0 vs -0).
console.log(Object.is(NaN, NaN)); // true
console.log(Object.is(0, -0)); // false
Determines if new properties can be added to an object.
const obj = {};
console.log(Object.isExtensible(obj)); // true
Object.preventExtensions(obj);
console.log(Object.isExtensible(obj)); // false
Determines if an object is sealed (no properties can be added or removed, but existing ones can be modified).
const obj = { a: 1 };
console.log(Object.isSealed(obj)); // false
Object.seal(obj);
console.log(Object.isSealed(obj)); // true
Determines if an object is frozen (no properties can be added, removed, or modified).
const obj = { a: 1 };
console.log(Object.isFrozen(obj)); // false
Object.freeze(obj);
console.log(Object.isFrozen(obj)); // true
Sealing, Freezing, and Extensibility
Method | Description |
---|---|
Object.freeze() | Freezes an object |
Object.seal() | Seals an object |
Object.preventExtensions() | Prevents new properties from being added to an object. |
Freezes an object, preventing properties from being added, removed, or modified.
const obj = { a: 1 };
Object.freeze(obj);
obj.a = 2; // Cannot modify, ignored in strict mode.
obj.b = 2; // Cannot add, ignored in strict mode.
console.log(obj); // { a: 1 }
Seals an object, preventing new properties from being added and marking all existing properties as non-configurable.
const obj = { a: 1 };
Object.seal(obj);
obj.b = 2; // Cannot add, ignored in strict mode.
delete obj.a; // Cannot delete, ignored in strict mode.
console.log(obj); // { a: 1 }
Prevents new properties from being added to an object.
const obj = { a: 1 };
Object.preventExtensions(obj);
obj.b = 2; // Cannot add, ignored in strict mode.
console.log(obj); // { a: 1 }
Other Utilities
Method | Description |
---|---|
Object.toString() | Returns a string that represents the object. |
Object.valueOf() | Returns the primitive value of the specified object. |
Object.assign() | Copies the enumerable properties from one or more source objects to another target object and returns it. |
Returns a string representing the object, useful for debugging.
const obj = {};
console.log(obj.toString()); // "[object Object]"
Returns the primitive value of the specified object (normally returns the object itself but can be overridden).
const obj = {};
console.log(obj.valueOf() === obj); // true