Skip to content

Instantly share code, notes, and snippets.

@nielk
Created October 29, 2014 08:57
Show Gist options
  • Save nielk/17bea9a61b2dd16f3c6d to your computer and use it in GitHub Desktop.
Save nielk/17bea9a61b2dd16f3c6d to your computer and use it in GitHub Desktop.
OOP javascript object inheritance constructor prototype
/**
* Animal constructor
**/
var Animal = function(name) {
this.name = name;
};
Animal.prototype.speak = function() {
console.log('my name is ' + this.name);
};
/**
* Dog constructor
**/
var Dog = function(name) {
Animal.call(this, name); // super
};
// inherite from Animal constructor
Dog.prototype = new Animal();
Dog.prototype.bark = function() {
console.log('wouf wouf');
};
var puppy = new Dog('alpine');
puppy.speak();
puppy.bark();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment