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

44 lines
1.2 KiB
JavaScript
Raw Normal View History

2010-07-29 04:51:35 +00:00
var Animal, Horse, Snake, sam, tom;
2010-11-21 17:38:27 +00:00
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
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;
child.__super__ = parent.prototype;
2010-11-21 17:38:27 +00:00
return child;
};
2010-11-21 17:38:27 +00:00
Animal = function() {
function Animal(name) {
this.name = name;
}
2010-11-21 17:38:27 +00:00
Animal.prototype.move = function(meters) {
return alert(this.name + " moved " + meters + "m.");
};
2010-10-20 01:59:13 +00:00
return Animal;
2010-11-21 17:38:27 +00:00
}();
Snake = function() {
2010-10-20 01:59:13 +00:00
function Snake() {
2010-11-21 17:38:27 +00:00
Snake.__super__.constructor.apply(this, arguments);
}
2010-11-21 17:38:27 +00:00
__extends(Snake, Animal);
Snake.prototype.move = function() {
alert("Slithering...");
return Snake.__super__.move.call(this, 5);
};
2010-10-20 01:59:13 +00:00
return Snake;
2010-11-21 17:38:27 +00:00
}();
Horse = function() {
2010-10-20 01:59:13 +00:00
function Horse() {
2010-11-21 17:38:27 +00:00
Horse.__super__.constructor.apply(this, arguments);
}
2010-11-21 17:38:27 +00:00
__extends(Horse, Animal);
Horse.prototype.move = function() {
alert("Galloping...");
return Horse.__super__.move.call(this, 45);
};
2010-10-20 01:59:13 +00:00
return Horse;
2010-11-21 17:38:27 +00:00
}();
2010-07-29 04:51:35 +00:00
sam = new Snake("Sammy the Python");
tom = new Horse("Tommy the Palomino");
sam.move();
tom.move();