Created
August 8, 2021 16:50
-
-
Save eamon0989/5dd1d3c6667e9aeca9f3680017ad908a to your computer and use it in GitHub Desktop.
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
class Grandfather { | |
constructor(name, age, job) { | |
this.name = name; | |
this.age = age; | |
this.job = job; | |
} | |
aboutMe() { | |
console.log(`I'm ${this.name}, I'm ${this.age} years old and I'm ${this.job === 'retired' ? '' : 'a '}${this.job}.`); | |
} | |
} | |
class Father extends Grandfather { | |
} | |
class Son extends Father { | |
} | |
let grandad = new Grandfather('Jonathan', 80, 'retired'); | |
grandad.aboutMe(); // I'm Jonathan, I'm 80 years old and I'm retired. | |
let dad = new Father('John', 40, 'coder'); | |
dad.aboutMe(); // I'm John, I'm 40 years old and I'm a coder. | |
let son = new Son('Johnny', 18, 'student'); | |
son.aboutMe(); // I'm Johnny, I'm 18 years old and I'm a student. | |
console.log(son instanceof Son); // true | |
console.log(son instanceof Grandfather); // true | |
console.log(Father.prototype.isPrototypeOf(Son.prototype)); // true | |
console.log(Grandfather.prototype.isPrototypeOf(Son.prototype)); // true | |
console.log(son); // Grandfather { name: 'Johnny', age: 18, job: 'student' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment