Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created November 21, 2011 21:31
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 thinkphp/1384012 to your computer and use it in GitHub Desktop.
Save thinkphp/1384012 to your computer and use it in GitHub Desktop.
Inheritance ruby-style
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