revision:
Object.assign() : copies the values of all enumerable own properties from one or more source objects to a target object.
syntax: const target = { a: 1 }; const source = { b: 2 }; const result = Object.assign(target, source); console.log(result); // { a: 1, b: 2 }
Object.create() : creates a new object with the specified prototype object and properties.
syntax: const personPrototype = { greet() { console.log(`Hello, I'm ${this.name}`); } }; const alice = Object.create(personPrototype); alice.name = 'Alice'; alice.greet(); // Hello, I'm Alice
Object.defineProperties() : adds the named properties described by the given descriptors to an object.
syntax: const obj = {}; Object.defineProperties(obj, { prop1: { value: 42, writable: false, enumerable: true, configurable: false }, prop2: { get() { return this._prop2; }, set(value) { this._prop2 = value; }, enumerable: true, configurable: true } });
Object.defineProperty() : adds the named property described by a given descriptor to an object.
syntax: const obj = {}; Object.defineProperty(obj, 'name', { value: 'Alice', writable: false, configurable: false }); obj.name = 'Bob'; console.log(obj.name); // Alice
Object.entries() : returns an array containing all of the [key, value] pairs of a given object's own enumerable string properties.
syntax: const obj = { name: 'Alice', age: 25 }; console.log(Object.entries(obj)); // [["name", "Alice"], ["age", 25]]
Object.freeze() : freezes an object. Other code cannot delete or change its properties.
syntax: const obj = { name: 'Alice' }; Object.freeze(obj); obj.name = 'Bob'; // No effect in strict mode console.log(obj.name); // Alice
Object.fromEntries() : returns a new object from an iterable of [key, value] pairs. (This is the reverse of Object.entries).
syntax: const entries = [['name', 'Alice'], ['age', 25]]; const obj = Object.fromEntries(entries); console.log(obj); // { name: 'Alice', age: 25 }
Object.getOwnPropertyDescriptor() : returns a property descriptor for a named property on an object.
syntax: const obj = { name: 'Alice' }; console.log(Object.getOwnPropertyDescriptor(obj, 'name')); // { value: 'Alice', writable: true, enumerable: true, configurable: true }
Object.getOwnPropertyDescriptors() : returns an object containing all own property descriptors for an object.
syntax: const obj = { firstName: 'John', lastName: 'Doe' }; Object.defineProperty(obj, 'fullName', { get() { return `${this.firstName} ${this.lastName}`; }, enumerable: true, configurable: true }); const descriptors = Object.getOwnPropertyDescriptors(obj); console.log(descriptors);
Object.getOwnPropertyNames() : returns an array containing the names of all of the given object's own enumerable and non-enumerable properties.
syntax: const obj = { name: 'Alice', age: 25 }; Object.defineProperty(obj, 'secret', { value: 42, enumerable: false }); console.log(Object.getOwnPropertyNames(obj)); // Output: [ 'name', 'age', 'secret' ]
Object.getOwnPropertySymbols() : returns an array of all symbol properties found directly upon a given object.
syntax: const sym1 = Symbol('id'); const sym2 = Symbol('email'); const user = { name: 'Alice', [sym1]: 123, [sym2]: '[email protected]' }; const symbols = Object.getOwnPropertySymbols(user); console.log(symbols); // Output: [ Symbol(id), Symbol(email) ] console.log(user[sym1]); // 123 console.log(user[sym2]); // [email protected]
Object.getPrototypeOf() : returns the prototype (internal [[Prototype]] property) of the specified object.
syntax: const proto = { greet: function() { console.log('Hello'); } }; const obj = {}; Object.setPrototypeOf(obj, proto); obj.greet(); // Hello
Object.groupBy() : groups the elements of a given iterable according to the string values returned by a provided callback function. The returned object has separate properties for each group, containing arrays with the elements in the group.
syntax: const people = [ { name: 'Alice', age: 25 }, { name: 'Bob', age: 30 }, { name: 'Eve', age: 25 } ]; const grouped = Object.groupBy(people, person => person.age); console.log(grouped);
Object.hasOwn() : returns true if the specified object has the indicated property as its own property, or false if the property is inherited or does not exist.
syntax: const user = { name: 'Alice', age: 25 }; console.log(Object.hasOwn(user, 'name')); // true console.log(Object.hasOwn(user, 'toString')); // false (inherited from Object.prototype)
Object.is() : compares if two values are the same value. Equates all NaN values (which differs from both IsLooselyEqual used by == and IsStrictlyEqual used by ===).
syntax: console.log(Object.is(NaN, NaN)); // true console.log(Object.is(+0, -0)); // false
Object.isExtensible() : determines if extending of an object is allowed.
syntax: const obj = { name: 'Alice' }; console.log(Object.isExtensible(obj)); // true Object.preventExtensions(obj); console.log(Object.isExtensible(obj)); // false obj.age = 30; console.log(obj.age); // undefined — adding new property fails silently
Object.isFrozen() : determines if an object was frozen.
syntax: const user = { name: 'Alice' }; console.log(Object.isFrozen(user)); // false Object.freeze(user); console.log(Object.isFrozen(user)); // true user.name = 'Bob'; console.log(user.name); // 'Alice' – assignment ignored silently (non-strict mode) user.age = 30; console.log(user.age); // undefined – property not added
Object.isSealed() : determines if an object is sealed.
syntax: const user = { name: 'Alice' }; console.log(Object.isSealed(user)); // false Object.seal(user); console.log(Object.isSealed(user)); // true user.name = 'Bob'; console.log(user.name); // 'Bob' – property value can be changed delete user.name; console.log(user.name); // 'Bob' – deletion fails silently in non-strict mode user.age = 30; console.log(user.age); // undefined – cannot add new properties
Object.keys() : returns an array containing the names of all of the given object's own enumerable string properties.
syntax: const obj = { name: 'Alice', age: 25 }; console.log(Object.keys(obj)); // ["name", "age"]
Object.preventExtensions() : prevents any extensions of an object.
syntax: const user = { name: 'Alice' }; console.log(Object.isExtensible(user)); // true Object.preventExtensions(user); console.log(Object.isExtensible(user)); // false // Can't add new properties user.age = 30; console.log(user.age); // undefined (fails silently in non-strict mode) // But can modify existing properties user.name = 'Bob'; console.log(user.name); // Bob // Can delete existing properties delete user.name; console.log(user.name); // undefined
Object.seal() : prevents other code from deleting properties of an object.
syntax: const obj = { name: 'Alice' }; Object.seal(obj); delete obj.name; // No effect obj.name = 'Bob'; console.log(obj.name); // Bob
Object.setPrototypeOf() : sets the object's prototype (its internal [[Prototype]] property).
syntax: const parent = { greet() { console.log('Hello!'); } }; const child = {}; // Set parent as the prototype of child Object.setPrototypeOf(child, parent); child.greet(); // Hello!
Object.values() : returns an array containing the values that correspond to all of a given object's own enumerable string properties.
syntax: const obj = { name: 'Alice', age: 25 }; console.log(Object.values(obj)); // ["Alice", 25]
<div> <p id="object1"></p> <p id="object2"></p> <p id="object3"></p> <p id="object4"></p> <p id="object5"></p> </div> <script> const object1 = { name: 'John', age: 20, }; document.getElementById("object1").innerHTML = "keys(): " + JSON.stringify(Object.keys(object1)); document.getElementById("object2").innerHTML = "values(): " + JSON.stringify(Object.values(object1)); const personA = { name: "Oscar", introduction: function() { document.getElementById("object3").innerHTML = "create() : " + `my name is ${this.name}`; console.log("create(): " +`My name is ${this.name}`); } }; const me = Object.create(personA); me.name = 'Theo'; me.introduction(); Object.freeze(personA); personA.name = "Filip"; console.log("freeze(): ", personA.name) document.getElementById("object4").innerHTML = "freeze() : " + personA.name; const personB = { name: "Filip", age : 20 }; const obj2 = { ishuman: true } Object.assign(personB, obj2); console.log(personB); document.getElementById("object5").innerHTML = "assign: " + JSON.stringify(personB); </script>