diff --git a/8-using-inheritance/examine-prototype.js b/8-using-inheritance/examine-prototype.js index e03e0d6..7fdbf8e 100644 --- a/8-using-inheritance/examine-prototype.js +++ b/8-using-inheritance/examine-prototype.js @@ -1,11 +1,23 @@ class Counter {} +Counter.prototype.count = 0; +Counter.prototype.increment = function () { + this.count += 1; +}; + const counter1 = new Counter(); + const counter2 = new Counter(); -const counter1Prototype = Reflect.getPrototypeOf(counter1); -const counter2Prototype = Reflect.getPrototypeOf(counter2); +console.log(`Prototype has: ${Object.keys(Reflect.getPrototypeOf(counter1))}`); +console.log(`Before increment, instance has: ${Object.keys(counter1)}`); + +console.log(counter1.count); +console.log(counter2.count); + +counter1.increment(); + +console.log(`After increment. instance has: ${Object.keys(counter1)}`); -console.log(counter1 === counter2); // false -console.log(counter1Prototype === counter2Prototype); // true -// Objects are different, but they share their prototypes +console.log(counter1.count); +console.log(counter2.count); diff --git a/8-using-inheritance/using-inheritance.md b/8-using-inheritance/using-inheritance.md index ee98024..be438e4 100644 --- a/8-using-inheritance/using-inheritance.md +++ b/8-using-inheritance/using-inheritance.md @@ -14,3 +14,7 @@ JavaScript implements **prototypal** inheritance - Object chaining - `Reflect.getPrototypeOf` + +## Behavior of Get vs. Set + +- Gets search deep, but sets are always shallow