Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created November 23, 2011 13:26
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/1388653 to your computer and use it in GitHub Desktop.
Save thinkphp/1388653 to your computer and use it in GitHub Desktop.
$extend(subClass,superClass)
var Human = function(name){
this.name = name;
this.isAlive = true;
this.energy = 1;
};
Human.prototype.eat = function() {
return this.energy++;
};
var Gibon = function(name) {
Gibon._base.call(this,name);
this.kills = 0;
};
GIB.extend(Gibon, Human);
Gibon.prototype.attack = function(target) {
target.isAlive = false;
this.kills++;
};
Gibon.prototype.eat = function() {
Gibon._super.eat.call(this);
return this.energy +=2;
};
var h = new Human('adrian');
var g = new Gibon("croco");
g.eat();
g.attack(h);
alert("g energy:" + g.energy);
//->4
alert("h isAlive: " + h.isAlive);
//->false
alert("g isAlive: " + g.isAlive);
//->true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment