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

44 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) {
2010-10-20 01:59:13 +00:00
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
2010-10-20 01:59:13 +00:00
child.prototype = new ctor;
if (typeof parent.extended === "function") parent.extended(child);
child.__super__ = parent.prototype;
};
2010-10-05 03:21:16 +00:00
Animal = (function() {
2010-10-20 01:59:13 +00:00
function Animal(_arg) {
2010-10-05 03:21:16 +00:00
this.name = _arg;
return this;
}
2010-10-20 01:59:13 +00:00
return Animal;
2010-10-05 03:21:16 +00:00
})();
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() {
2010-10-20 01:59:13 +00:00
function Snake() {
2010-10-05 03:21:16 +00:00
return Animal.apply(this, arguments);
}
2010-10-20 01:59:13 +00:00
return Snake;
2010-10-05 03:21:16 +00:00
})();
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() {
2010-10-20 01:59:13 +00:00
function Horse() {
2010-10-05 03:21:16 +00:00
return Animal.apply(this, arguments);
}
2010-10-20 01:59:13 +00:00
return Horse;
2010-10-05 03:21:16 +00:00
})();
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();