From 78deb5b5a50432731f06d115e98d3a210ca922e4 Mon Sep 17 00:00:00 2001 From: Aaron Nguyen Date: Mon, 7 Feb 2022 16:46:19 -0500 Subject: [PATCH] Start Chapter 8. Using Inheritance --- 8-using-inheritance/examine-prototype.js | 11 +++++++++++ 8-using-inheritance/using-inheritance.md | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 8-using-inheritance/examine-prototype.js create mode 100644 8-using-inheritance/using-inheritance.md diff --git a/8-using-inheritance/examine-prototype.js b/8-using-inheritance/examine-prototype.js new file mode 100644 index 0000000..e03e0d6 --- /dev/null +++ b/8-using-inheritance/examine-prototype.js @@ -0,0 +1,11 @@ +class Counter {} + +const counter1 = new Counter(); +const counter2 = new Counter(); + +const counter1Prototype = Reflect.getPrototypeOf(counter1); +const counter2Prototype = Reflect.getPrototypeOf(counter2); + +console.log(counter1 === counter2); // false +console.log(counter1Prototype === counter2Prototype); // true +// Objects are different, but they share their prototypes diff --git a/8-using-inheritance/using-inheritance.md b/8-using-inheritance/using-inheritance.md new file mode 100644 index 0000000..ee98024 --- /dev/null +++ b/8-using-inheritance/using-inheritance.md @@ -0,0 +1,16 @@ +# Chapter 8. Using Inheritance + +JavaScript implements **prototypal** inheritance + +## Understanding Prototypal Inheritance + +- Prototypal inheritance is implemented using delegation +- Delegation is better than inheritance +- Instead of relying on base class or superclass, prototypal inheritance relies on an object next in the chain to serve as its base +- Prototypal inheritance is dynamic which allows altering object that serves as the base at runtime +- Base object is called the object's prototype + +## Prototype Chain + +- Object chaining +- `Reflect.getPrototypeOf`