Skip to content

Instantly share code, notes, and snippets.

@Kichrum

Kichrum/inherit.js

Last active Aug 29, 2015
Embed
What would you like to do?
Inheritance in JavaScript for EcmaScript 3
/**
* One more version of inheritance for ES3
* @author Kichrum
*/
var inherit = function(Parent) {
var Child = function(){ Parent.apply(this, arguments) }
// We can use
// Child.prototype = Object.create(Parent.prototype)
// here for ES5 instead of next 3 lines:
var F = function() {}
F.prototype = Parent.prototype
Child.prototype = new F()
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.