The Object


Merging objects

In es6 you can merge objects easily with Object.assign()

Notes

  • Merges objects with the 'base' object being the first argument passed into the method
  • you can pass in a new empty object to be merged into

Example One : Assign

//ES6 built in object extensions

var obj1 = {
  a: 1
}

var obj2 = {
  b: 2
}

var obj = Object.assign(obj1, obj2)

console.log(obj)


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

Example One : Assign - Empty Object

//ES6 built in object extensions

var obj1 = {
  a: 1
}

var obj2 = {
  b: 2
}

var obj = Object.assign({}, obj1, obj2)

console.log(obj)


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

Object setPrototypeOf

//ES6 built in object extensions - setPrototypeOf

let princess = {
  name : 'Annabelle'
};

let dog = {
  name : 'Broccoli'
}

//Before setting the prototype
console.log(princess.__proto__);
console.log(princess.__proto__ === Object.prototype);

// With setPrototype you can set the prototype of the object
// allows you to change the prototype of a object after it's been created

Object.setPrototypeOf(princess, dog);


//After overwriting the origional prototype
console.log(princess.__proto__);
console.log(princess.__proto__ === Object.prototype);
console.log(princess.__proto__ === dog);
console.log(princess.name)



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Last Updated: 8/13/2019, 6:55:26 PM