Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.