Last active
August 29, 2015 14:09
-
-
Save shaunwallace/6b56f49ba42e786caf22 to your computer and use it in GitHub Desktop.
Classical Inheritance
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
// constructor function to create the parent | |
function Parent( name ){ | |
this.name = name || 'Bob'; | |
} | |
// extending the parent | |
Parent.prototype.sayHi = function() { | |
return 'Hi from ' + this.name; | |
} | |
// empty child constructor function | |
function Child() {}; | |
// child will now inherit from the parent by pointing it's prototype to an instance of the parent | |
Child.prototype = new Parent(); | |
// create instance of the child | |
var childInstance = new Child(); | |
// call a method on the parent from the child instance | |
childInstance.sayHi(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment