Created
July 22, 2014 07:32
-
-
Save Kichrum/da055d81b333b554c9c6 to your computer and use it in GitHub Desktop.
Inheritance in JavaScript for EcmaScript 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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