Skip to content

Commit

Permalink
Done Chapter 8. Using Inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Nguyen committed Mar 1, 2022
1 parent 9e4b5cc commit 9564df4
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
12 changes: 12 additions & 0 deletions 8-using-inheritance/array-vs-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class MyString extends String {}
class MyArray extends Array {
static get [Symbol.species]() {
return Array;
}
}

const concString = new MyString().concat(new MyString());
const concArray = new MyArray().concat(new MyArray());

console.log(`instanceof MyString?: ${concString instanceof MyString}`);
console.log(`instanceof MyArray?: ${concArray instanceof MyArray}`);
7 changes: 7 additions & 0 deletions 8-using-inheritance/inherit-from-function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function LegacyClass(value) {
this.value = value;
}

class NewClass extends LegacyClass {}

console.log(new NewClass(1));
9 changes: 9 additions & 0 deletions 8-using-inheritance/inheritance.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class Person {
}
}

class AwesomePerson extends Person {
get fullName() {
return `Awesome ${super.fullName}`;
}
}

class ReputablePerson extends Person {
constructor(firstName, lastName, rating) {
super(firstName, lastName);
Expand All @@ -33,3 +39,6 @@ class ReputablePerson extends Person {
const alan = new ReputablePerson("Alan", "Turing", 5);
console.log(alan.toString());
console.log(alan.fullName);

const ball = new AwesomePerson("Lucille", "Ball");
console.log(ball.fullName);
33 changes: 33 additions & 0 deletions 8-using-inheritance/managing-instaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Names {
constructor(...names) {
this.names = names;
}

filter1(selector) {
return new Names(...this.names.filter(selector));
}

filter2(selector) {
const constructor = Reflect.getPrototypeOf(this).constructor;
return new constructor(...this.names.filter(selector));
}

filter3(selector) {
const constructor =
Reflect.getPrototypeOf(this).constructor[Symbol.species] ||
Reflect.getPrototypeOf(this).constructor;

return new constructor(...this.names.filter(selector));
}
}

class SpecializedNames extends Names {
static get [Symbol.species]() {
return Names;
}
}

const specializedNames = new SpecializedNames("Java", "C#", "JavaScript");
console.log(specializedNames.filter1((name) => name.startsWith("Java")));
console.log(specializedNames.filter2((name) => name.startsWith("Java")));
console.log(specializedNames.filter3((name) => name.startsWith("Java")));
50 changes: 50 additions & 0 deletions 8-using-inheritance/prototypal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
toString() {
return `Name: ${this.firstName} ${this.lastName}`;
}
get fullName() {
return `${this.firstName} ${this.lastName}`;
}
get lastName() {
return this._lastName;
}
set lastName(value) {
this._lastName = value;
}
}

class ReputablePerson extends Person {
constructor(firstName, lastName, rating) {
super(firstName, lastName);
this.rating = rating;
}
toString() {
return `${super.toString()} Rating: ${this.rating}`;
}
get fullName() {
return `Reputed ${this.lastName}, ${super.fullName}`;
}
}

const printPrototypeHierarchy = function (instance) {
if (instance !== null) {
console.log(instance);
printPrototypeHierarchy(Reflect.getPrototypeOf(instance));
}
};

const alan = new ReputablePerson("Alan", "Turing", 5);
printPrototypeHierarchy(alan);

class ComputerWiz {}

Reflect.setPrototypeOf(Reflect.getPrototypeOf(alan), ComputerWiz.prototype);
console.log("...after change of prototpye...");
printPrototypeHierarchy(alan);

const ada = new ReputablePerson("Ada", "Lovelace", 5);
printPrototypeHierarchy(ada);
7 changes: 7 additions & 0 deletions 8-using-inheritance/using-inheritance.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,10 @@ JavaScript implements **prototypal** inheritance

- Extending a class
- Overriding methods
- `extends` implies prototypal inheritance
- `object.prototype` is different from `Reflect.getPrototypeOf(object)` https://stackoverflow.com/q/38740610
- `object.prototype` is instance's prototype. `Reflect.getPrototypeOf(object)` is prototype of **instance's parent**
- Changing the prototype chain using `Reflect.setPrototypeOf()`
- Using default constructors
- Extending legacy classes
- Managing instance types with species

0 comments on commit 9564df4

Please sign in to comment.