Skip to content

Instantly share code, notes, and snippets.

@shaunwallace
Last active August 29, 2015 14:09
Show Gist options
  • Save shaunwallace/6b56f49ba42e786caf22 to your computer and use it in GitHub Desktop.
Save shaunwallace/6b56f49ba42e786caf22 to your computer and use it in GitHub Desktop.
Classical Inheritance
// 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