-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprototypal.js
50 lines (44 loc) · 1.18 KB
/
prototypal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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);