Skip to content

Instantly share code, notes, and snippets.

@Kichrum
Last active August 29, 2015 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kichrum/fa80aa9f20df2b0d3584 to your computer and use it in GitHub Desktop.
Save Kichrum/fa80aa9f20df2b0d3584 to your computer and use it in GitHub Desktop.
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