-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Aaron Nguyen
committed
Mar 1, 2022
1 parent
9e4b5cc
commit 9564df4
Showing
6 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters