Skip to content

Latest commit

 

History

History
31 lines (22 loc) · 886 Bytes

object_assign.md

File metadata and controls

31 lines (22 loc) · 886 Bytes

Object.assign

Object.assign lets us merge one object's properties into another, replacing values of properties with matching names. We can use this to copy an object's values without altering the existing one.

let movie1 = {
  name: 'Star Wars',
  episode: 7
};

let movie2 = Object.assign({}, movie1);

movie2.episode = 8;

console.log(movie1.episode); // writes 7
console.log(movie2.episode); // writes 8

As you can see, although we have some way of copying an object, we haven't made it immutable, since we were able to set the episode's property to 8. Also, how do we modify the episode property in this case? We do that through the assign call:

let movie1 = {
  name: 'Star Wars',
  episode: 7
};

let movie2 = Object.assign({}, movie1, { episode: 8 });

console.log(movie1.episode); // writes 7
console.log(movie2.episode); // writes 8