-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy path4-Prototype-Chains.js
39 lines (30 loc) · 1.36 KB
/
4-Prototype-Chains.js
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
29
30
31
32
33
34
35
36
37
38
// HOW TO USE THESE NOTES:
// Use these notes to follow along with the lesson. You can run the code in this file to reproduce what you see in the videos.
// Prototype Chains
var gold = {a:1};
console.log(gold.a); // 1
console.log(gold.z); // undefined
function extend(destination, source) { // built an extend fcn
var copy = destination;
for (var key in source) {
destination[key] = source[key];
}
return copy;
} // { a: 1 }
var blue = extend({}, gold); // helper function extend
blue.b = 2;
console.log(blue.a); // 1
console.log(blue.b); // 2
var rose = Object.create(gold);
rose.b = 2;
console.log(rose.a); // 1 -> result of lookup to prototype obj gold
console.log(rose.b); // 2 -> does not require lookup bc b exists in local context
console.log(rose.z); // undefined -> cannot be found in rose or in prototype obj
// now let's add a z property to the gold object
gold.z = 3;
console.log(blue.z); // undefined -> no delegation relationship exists btw blue and gold
console.log(rose.z); // 3 -> lookup falls to the prototype
// The Object Prototype - provides shared properties of ALL objects
rose.toString(); // example of a shared property on the Object prototype
// Constructor property makes it easy to tell what fcn was used to create an object.
// .constructor on the Object prototype points to a different object (a constructor function) that's stored elsewhere.