Created
November 21, 2011 21:31
-
-
Save thinkphp/1384012 to your computer and use it in GitHub Desktop.
Inheritance ruby-style
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
def ("Human") ({ | |
init: function(name) { | |
this.name = name; | |
this.isAlive = true; | |
this.energy = 1; | |
}, | |
eat: function(){ | |
this.energy++; | |
} | |
}); | |
def ("Gibon") < Human({ | |
init: function(name){ | |
this._super(name); | |
}, | |
kills: 0, | |
attack: function(target) { | |
target.isAlive = false; | |
this.kills++; | |
} | |
}) | |
var adrian = new Human("Adrian"); | |
adrian.eat(); | |
console.log(adrian.energy); | |
var liviu = new Gibon("Liviu"); | |
liviu.attack(adrian); | |
console.log(adrian.isAlive); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment