Skip to content

Instantly share code, notes, and snippets.

@ositowang
Created April 5, 2019 16:45
Show Gist options
  • Save ositowang/afef0e3f3e582434c34f343b804739b6 to your computer and use it in GitHub Desktop.
Save ositowang/afef0e3f3e582434c34f343b804739b6 to your computer and use it in GitHub Desktop.
Inheritance based on combination
/**
* Combination inheritance. As the name says, we combine the two ways.
* 1. We have two copies of the superclass properties. One copied from the super
* class constructor. The other we copied from the prototype.
*/
function superDaddy(name) {
this.name = name;
this.dogs = ['teddy', 'cookie', 'variable'];
}
superDaddy.prototype.sayHello = function() {
console.log('Hello');
};
function superSon(name, age) {
superDaddy.call(this, name);
this.age = age;
}
superSon.prototype = new superDaddy();
superSon.prototype.constructor = superSon;
superSon.prototype.sayGoodBye = function() {
console.log(this.name + ' :Bye bye');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment