Skip to content

Instantly share code, notes, and snippets.

@eamon0989
Created August 8, 2021 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eamon0989/5dd1d3c6667e9aeca9f3680017ad908a to your computer and use it in GitHub Desktop.
Save eamon0989/5dd1d3c6667e9aeca9f3680017ad908a to your computer and use it in GitHub Desktop.
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