Created
April 5, 2019 16:45
-
-
Save ositowang/afef0e3f3e582434c34f343b804739b6 to your computer and use it in GitHub Desktop.
Inheritance based on combination
This file contains hidden or 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
/** | |
* 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