jashkenas--coffeescript/documentation/js/classes.js

42 lines
1.1 KiB
JavaScript
Raw Normal View History

2010-07-29 04:51:35 +00:00
var Animal, Horse, Snake, sam, tom;
var __extends = function(child, parent) {
var ctor = function() {};
ctor.prototype = parent.prototype;
child.prototype = new ctor();
child.prototype.constructor = child;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
};
2010-10-05 03:21:16 +00:00
Animal = (function() {
return function Animal(_arg) {
this.name = _arg;
return this;
};
})();
2010-07-29 04:51:35 +00:00
Animal.prototype.move = function(meters) {
return alert(this.name + " moved " + meters + "m.");
};
2010-10-05 03:21:16 +00:00
Snake = (function() {
return function Snake() {
return Animal.apply(this, arguments);
};
})();
2010-07-29 04:51:35 +00:00
__extends(Snake, Animal);
Snake.prototype.move = function() {
alert("Slithering...");
2010-08-24 02:08:33 +00:00
return Snake.__super__.move.call(this, 5);
2010-07-29 04:51:35 +00:00
};
2010-10-05 03:21:16 +00:00
Horse = (function() {
return function Horse() {
return Animal.apply(this, arguments);
};
})();
2010-07-29 04:51:35 +00:00
__extends(Horse, Animal);
Horse.prototype.move = function() {
alert("Galloping...");
2010-08-24 02:08:33 +00:00
return Horse.__super__.move.call(this, 45);
2010-07-29 04:51:35 +00:00
};
sam = new Snake("Sammy the Python");
tom = new Horse("Tommy the Palomino");
sam.move();
tom.move();