Skip to content

Commit

Permalink
Study Behavior of Get vs. Set
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Nguyen committed Feb 7, 2022
1 parent 78deb5b commit 3be65bb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
22 changes: 17 additions & 5 deletions 8-using-inheritance/examine-prototype.js
Original file line number Diff line number Diff line change
@@ -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);
4 changes: 4 additions & 0 deletions 8-using-inheritance/using-inheritance.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3be65bb

Please sign in to comment.