Let\'s say I have the following code snippet.
function test(id) { alert(id); }
testChild.prototype = new test();
function testChild(){}
var instance = new
That's how you do this in CoffeeScript:
class Test
constructor: (id) -> alert(id)
class TestChild extends Test
instance = new TestChild('hi')
Nope, I'm not starting a holy war. Instead, I'm suggesting to take a look at resulting JavaScript code to see how subclassing could be implemented:
// Function that does subclassing
var __extends = function(child, parent) {
for (var key in parent) {
if (Object.prototype.hasOwnProperty.call(parent, key)) {
child[key] = parent[key];
}
}
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child;
};
// Our code
var Test, TestChild, instance;
Test = function(id) { alert(id); };
TestChild = function() {
TestChild.__super__.constructor.apply(this, arguments);
}; __extends(TestChild, Test);
instance = new TestChild('hi');
// And we get an alert
See it in action at http://jsfiddle.net/NGLMW/3/.
To stay correct, the code is slightly modified and commented to be more readable, compared to CoffeeScript output.