Javascript Objects

Javascript Object methods that are essential to know

Javascript Object

1. Object.create(proto, [propertiesObject])

produces a new object with the optional properties and prototype that have been specified.
helpful in establishing inheritance.

const person = { isHuman: false };
const me = Object.create(person);
me.name = "Alice";

2. Object.assign(target, …sources)

returns the updated target after copying properties from one or more source objects to a target object.
Excellent for combining objects and shallow cloning

const target = { a: 1 };
const source = { b: 2 };
const merged = Object.assign(target, source); // { a: 1, b: 2 }

3. Object.keys(obj)

gives back an array containing the names of the object’s enumerable properties (keys).
Good for iterating through keys.

const person = { name: "Alice", age: 25 };
console.log(Object.keys(person)); // ["name", "age"]

4. Object.values(obj)

gives back an array containing the values of the object’s enumerable properties.
helpful for repeatedly accessing values without keys.

const person = { name: "Alice", age: 25 };
console.log(Object.values(person)); // ["Alice", 25]

5. Object.entries(obj)

The object’s own enumerable attributes are returned as an array of key-value pairs.
helpful for converting items into arrays for mapping or iteration.

const person = { name: "Alice", age: 25 };
console.log(Object.entries(person)); // [["name", "Alice"], ["age", 25]]

6. Object.fromEntries(iterable)

returns an object from an iterable of key-value pairs (such as arrays or maps).
helpful for reassembling things from data that has been altered.

const entries = [["name", "Alice"], ["age", 25]];
const person = Object.fromEntries(entries); // { name: "Alice", age: 25 }

7. Object.freeze(obj)

stops new properties from being added and existing properties from being changed.
beneficial for producing unchangeable items.

const obj = { name: "Alice" };
Object.seal(obj);
obj.age = 25; // Not allowed
obj.name = "Bob"; // Allowed

8. Object.seal(obj)

permits the modification of existing properties but prohibits the addition of new ones.
useful for restricting the structure of objects.

const obj = { name: "Alice" };
Object.seal(obj);
obj.age = 25; // Not allowed
obj.name = "Bob"; // Allowed

9. Object.hasOwn(obj, prop)

If the property is defined on the object directly, it returns true.
helpful for verifying object attributes without being influenced by inheritance.

const person = { name: "Alice" };
console.log(Object.hasOwn(person, "name")); // true

10. Object.getOwnPropertyNames(obj)

returns an array containing every property—including non-enumerable ones—that can be discovered directly on an object.
useful for looking into all of an object’s properties.

const obj = Object.create({}, {
    hidden: { value: "secret", enumerable: false },
    visible: { value: "shown", enumerable: true }
});
console.log(Object.getOwnPropertyNames(obj)); // ["hidden", "visible"]

11. Object.getPrototypeOf(obj)

gives back the prototype of the given object, which is the value of the internal [[Prototype]] attribute.
beneficial for manipulating prototypes and inheritance.

const obj = {};
const proto = Object.getPrototypeOf(obj); // Object.prototype

12. Object.setPrototypeOf(obj, prototype)

sets an object’s prototype to null or another object.
beneficial for dynamically changing the prototype chain, while performance costs frequently deter people from using it.

const newProto = { greet() { return "Hello!"; } };
const obj = {};
Object.setPrototypeOf(obj, newProto);
console.log(obj.greet()); // "Hello!"

13. Object.defineProperty(obj, prop, descriptor)

uses a property descriptor to define a new property or change an existing one on an object.
vital for accurate management of property attributes (e.g., enumerable, configurable, and writable).

const person = {};
Object.defineProperty(person, "name", { value: "Alice", writable: false });

14. Object.defineProperties(obj, props)

defines a number of an object’s properties.
helpful for configuring complicated objects with certain settings.

const person = {};
Object.defineProperties(person, {
    name: { value: "Alice", writable: true },
    age: { value: 30, writable: false }
});

15. Object.is(value1, value2)

checks to see if two values are the same. It handles NaN appropriately, in contrast to ===.
useful for equality checks where -0 or NaN differences are significant.

console.log(Object.is(NaN, NaN)); // true
console.log(Object.is(0, -0)); // false

From simple property manipulation to making immutable objects and working with prototypes, these methods address the most frequent requirements when working with JavaScript objects.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top