I don't think you can write a Coffeescript class which compiles to that Javascript (or something close). The Coffeescript class
insists on 2 things:
It ends the class body with return AwesomeObject;
If I put a return bar
in the class body, it objects with error: Class bodies cannot contain pure statements
.
The linked Javascript model is:
var AwesomeObject = (function() {
var AwesomeObject = function() {...};
...
return function() {
var o = new AwesomeObject();
...};
})();
It defines an AwesomeObject
constructor internally, but returns a different function. This is clearer if the internal name is changed to AwesomeObject1
. It functions the same, but there is no way of accessing AwesomeObject1
directly.
Also AwesomeObject()
and new AwesomeObject()
return the same thing.
{ [Function]
whatstuff: 'really awesome',
doStuff: [Function] }
The compiled Coffeescript (for class AwesomeObject...
) instead is:
AwesomeObject = (function() {
function AwesomeObject() {...}
...
return AwesomeObject;
})();
P.S.
https://github.com/jashkenas/coffee-script/issues/861
Coffeescript issue discussion on new Foo()
versus Foo()
syntax. Consensus seems to be that while new-less
calls are allowed in Javascript for objects like Date
, it isn't encouraged for user defined classes. This is interesting, though not really relevant to the question here.