Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. // Let's create an object o from function f with its own properties a and b:
  2. let f = function () {
  3. this.a = 1;
  4. this.b = 2;
  5. }
  6. let o = new f(); // {a: 1, b: 2}
  7.  
  8. // add properties in f function's prototype
  9. f.prototype.b = 3;
  10. f.prototype.c = 4;
  11.  
  12. // do not set the prototype f.prototype = {b:3,c:4}; this will break the prototype chain
  13. // o.[[Prototype]] has properties b and c.
  14. // o.[[Prototype]].[[Prototype]] is Object.prototype.
  15. // Finally, o.[[Prototype]].[[Prototype]].[[Prototype]] is null.
  16. // This is the end of the prototype chain, as null,
  17. // by definition, has no [[Prototype]].
  18. // Thus, the full prototype chain looks like:
  19. // {a: 1, b: 2} ---> {b: 3, c: 4} ---> Object.prototype ---> null
  20.  
  21. console.log(o.a); // 1
  22. // Is there an 'a' own property on o? Yes, and its value is 1.
  23.  
  24. console.log(o.b); // 2
  25. // Is there a 'b' own property on o? Yes, and its value is 2.
  26. // The prototype also has a 'b' property, but it's not visited.
  27. // This is called Property Shadowing
  28.  
  29. console.log(o.c); // 4
  30. // Is there a 'c' own property on o? No, check its prototype.
  31. // Is there a 'c' own property on o.[[Prototype]]? Yes, its value is 4.
  32.  
  33. console.log(o.d); // undefined
  34. // Is there a 'd' own property on o? No, check its prototype.
  35. // Is there a 'd' own property on o.[[Prototype]]? No, check its prototype.
  36. // o.[[Prototype]].[[Prototype]] is Object.prototype and there is no 'd' property by default, check its prototype.
  37. // o.[[Prototype]].[[Prototype]].[[Prototype]] is null, stop searching
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement