Skip to content

Instantly share code, notes, and snippets.

@ktajpuri
Created August 13, 2018 08:10
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 ktajpuri/461359e35f730f8372e7e62dcad25c63 to your computer and use it in GitHub Desktop.
Save ktajpuri/461359e35f730f8372e7e62dcad25c63 to your computer and use it in GitHub Desktop.
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
printIdentity() {
return "Mr. " + this.name;
}
testFunc() {
console.log("I am test from person");
}
}
class Student extends Person {
constructor(std, name, age) {
super(name, age);
this.std = std;
}
printGreeting() {
return super.printIdentity() + " is in standard " + this.std;
}
testFunc() {
console.log("I am test from student");
}
}
class HeadBoy extends Student {
constructor(house, std, name, age) {
super(std, name, age);
this.house = house;
}
print() {
return (
super.printGreeting() +
" and is in house " +
this.house +
" he is " +
this.age +
" years old"
);
}
/*testFunc() {//uncommenting this function will invoke test func from headboy
console.log('I am test from head boy')
}*/
}
let headboy = new HeadBoy("gandhi", "10th", "kamlesh", 20);
headboy.testFunc();
@ktajpuri
Copy link
Author

Prototype chain for this -
image

@ktajpuri
Copy link
Author

Interesting this to note here is that ES6 class keyword is just syntactical sugar over the JavaScript prototypical inheritance.

@ktajpuri
Copy link
Author

ktajpuri commented Aug 13, 2018

another interesting point is that if you define functions outside of constructor like its done in this this example, all those functions will be available on the prototypes and not on the object directly.
image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment