What are the differences between JavaScript's Object.create() and Object.assign() methods?
Antek N
antek n profile pic

TheObject.create() andObject.assign() methods in JavaScript are used for different purposes and have distinct differences. Let's explore each method and understand their characteristics: Object.create(): TheObject.create() method creates a new object and sets its prototype to a specified object. It allows you to create an object that inherits properties and methods from a prototype object. Here are some key points aboutObject.create(): 1. Prototype Inheritance:Object.create() takes an existing object as a parameter and creates a new object with the specified object as its prototype. This establishes prototype-based inheritance, where the new object inherits properties and methods from its prototype. 2. Object Composition:Object.create() is useful for implementing object composition rather than class-based inheritance. You can create a new object and compose it with desired properties and behaviors by assigning a prototype object that contains those properties and behaviors. 3. Null Prototype: If you want to create an object without inheriting any properties or methods, you can passnull as the parameter toObject.create(). This creates an object with no prototype chain. 4. Additional Properties: The properties and methods of the created object can be added or modified independently of the prototype object. Changes made to the prototype object do not affect existing objects created withObject.create(). Example usage ofObject.create():

1
2
3
4
5
6
7
8
9
10

const person = {
  greet() {
    console.log(`Hello, ${this.name}!`);
  }
};

const john = Object.create(person);
john.name = "John";
john.greet(); // Output: Hello, John!

In this example,person is the prototype object with agreet() method.Object.create(person) creates a new objectjohn withperson as its prototype.john inherits thegreet() method fromperson. Object.assign(): TheObject.assign() method is used to copy the values of enumerable properties from one or more source objects to a target object. It allows you to merge objects or add properties to an existing object. Here are some key points aboutObject.assign(): 1. Property Assignment:Object.assign() assigns properties and their values from the source objects to the target object. If a property exists in both the target and source objects, the value in the source object overwrites the value in the target object. 2. Shallow Copy:Object.assign() performs a shallow copy, meaning that nested objects or arrays are copied by reference. Changes to the nested objects or arrays in the source or target objects affect both objects. 3. Multiple Sources: You can provide multiple source objects toObject.assign(), and their properties will be merged into the target object in the order specified. 4. Target Object Modification:Object.assign() modifies the target object directly. It returns the modified target object after the properties from the source objects are copied. Example usage ofObject.assign():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

const person = {
  name: "John",
  age: 30
};

const details = {
  occupation: "Developer",
  country: "USA"
};

const john = {};
Object.assign(john, person, details);

console.log(john); // Output: { name: "John", age: 30, occupation: "Developer", country: "USA" }

In this example,person anddetails are source objects.Object.assign(john, person, details) copies the properties fromperson anddetails into thejohn object. To summarize,Object.create() is used for prototype-based inheritance and object composition, whileObject.assign() is used for merging properties from multiple source objects into a target object. They serve different purposes and are used in different scenarios based on the desired behavior.

Similar Questions

What are the differences between JavaScript's Object.freeze() and Object.seal() methods?

What are the differences between JavaScript's Object.assign() and Object spread?

What are the differences between JavaScript's Object.create() and new Object()?

What are the differences between JavaScript's Object.keys() and Object.values()?

What are the differences between Object.freeze() and Object.seal() in JavaScript?

What are the differences between Object.assign() and the spread operator in JavaScript?

What are the differences between JavaScript's splice() and slice() methods?

What are the differences between JavaScript's splice() and slice() methods?

What are the differences between JavaScript's push() and concat() methods?

What are the differences between JavaScript's filter() and find() methods?

What are the differences between Object.keys() and Object.values() in JavaScript?

What are the differences between JavaScript's Array.concat() and Array.join() methods?

What are the differences between the map() and filter() methods in JavaScript?

What are the differences between JavaScript's Array.filter() and Array.reduce() methods?

What are the differences between JavaScript's Array.concat() and Array.push() methods?

What are the differences between JavaScript's Array.from() and Array.of() methods?

What are the differences between JavaScript's Array.filter() and Array.map() methods?

What are the differences between JavaScript's Array.some() and Array.every() methods?

What are the differences between JavaScript's Array.push() and Array.pop() methods?

What are the differences between JavaScript's Array.map() and Array.flatMap() methods?