Created
October 12, 2015 23:18
-
-
Save zheplusplus/883241f691f111448d91 to your computer and use it in GitHub Desktop.
This file contains 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
function $extend(inherit, b) { | |
var base = b.$class; | |
for (var m in base) { | |
if (base.hasOwnProperty(k)) { | |
inherit[k] = base[k]; | |
} | |
} | |
function ctor() { | |
this.constructor = inherit; | |
} | |
ctor.prototype = base.prototype; | |
inherit.prototype = new ctor(); | |
inherit.$super = base.prototype; | |
return inherit; | |
} | |
var $AnimalClass, $Animal, $SnakeClass, $Snake, s, bf, bw; | |
$Animal = (function() { | |
function Animal(name) { | |
this.name = name; | |
} | |
Animal.prototype.move = function(x) { | |
return this.name + ' moved ' + x + ' meters'; | |
} | |
function create(name) { | |
return new Animal(name); | |
} | |
create.$class = Animal; | |
return create; | |
})(); | |
$Snake = (function() { | |
function Snake(name) { | |
return Snake.$super.constructor.apply(this, [name]); | |
} | |
$extend(Snake, $Animal); | |
Snake.prototype.move = function(x) { | |
return 'Snake ' + Snake.$super.move.call(this, x); | |
} | |
function create(name) { | |
return new Snake(name); | |
} | |
create.$class = Snake; | |
return create; | |
})(); | |
$Bird = function() { | |
function Bird(name, flyable) { | |
Bird.$super.constructor.apply(this, [name]); | |
this.flyable = flyable; | |
} | |
$extend(Bird, $Animal); | |
Bird.prototype.move = function(x) { | |
if (this.flyable) { | |
return this.fly(x); | |
} | |
return Bird.$super.move.call(this, x); | |
}; | |
Bird.prototype.fly = function(x) { | |
return this.name + ' flied ' + x + ' meters'; | |
}; | |
function create(name, flyable) { | |
return new Bird(name, flyable); | |
} | |
create.$class = Bird; | |
return create; | |
}(); | |
s = $Snake('Alche'); | |
console.log(s, s.move(3)); | |
bw = $Bird('Duck', false); | |
console.log(bw, bw.move(5)); | |
bf = $Bird('Hawk', true); | |
console.log(bf, bf.move(7)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment