Re:CoffeeScript, a little language that compiles to JavaScri...
by vr on Friday December 25, 2009 @03:26AM
(#1014865)
It seems you should use "Horse extends Animal.prototype" in your examples or just make "Horse extends Animal" compile to "Horse.prototype.__proto__ = Animal.prototype". Also a subclass constructor should call the one of the superclass. Otherwise, a single Animal instance is shared by all subclass instances and for no reason Animal is created simply to declare a subclass.
Re:CoffeeScript, a little language that compiles to JavaScri...
by tlrobinson on Friday December 25, 2009 @03:21AM
(#1014861)
Be careful with __proto__, it's non-standard and will break in some engines.
Re:CoffeeScript, a little language that compiles to JavaScri...
by vr on Friday December 25, 2009 @03:30AM
(#1014866)
To get the equivalent of __proto__ that works in every engine see goog.inherits from Closure.
Re:CoffeeScript, a little language that compiles to JavaScri...
by jashkenas on Friday December 25, 2009 @05:26PM
(#1015431)
Thanks for the pointer. I've pushed a commit that avoids __proto__, and produces this...
CoffeeScript:
Horse extends Animal
JavaScript: Horse.__superClass__ = Animal.prototype;
Horse.prototype = new Animal();
Horse.prototype.constructor = Horse;
Calling super uses the __superClass__ reference. I hate to add it as an enumerable property, but there doesn't seem to be any other way to get at it, and at least it's on the class and not the object.
http://jashkenas.github.com/coffee-script/#inheritance
It's out with version 0.1.2 now.