Skip to content

Instantly share code, notes, and snippets.

@Kichrum
Created July 22, 2014 07:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Kichrum/da055d81b333b554c9c6 to your computer and use it in GitHub Desktop.
Save Kichrum/da055d81b333b554c9c6 to your computer and use it in GitHub Desktop.
Inheritance in JavaScript for EcmaScript 5
/**
* Inheritance in ES5
* @author Kichrum
*/
var inherit = function(Parent) {
var Child = function(){ Parent.apply(this, arguments) }
Child.prototype = Object.create(Parent.prototype)
return Child
}
var Animal = function(name) { this.name = name }
Animal.prototype.say = function() { console.log('an ' + this.name) }
var Dog = inherit(Animal)
Dog.prototype.say = function() { console.log('dog ' + this.name) }
var a = new Animal('a')
var b = new Dog('b')
a.say()
b.say()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment