jashkenas/coffee-script — Issue #2111: Default subclass constructor should `return` result of super's constructor
Author: nathansobo
Created: 2012-02-08 16:38:24+00:00
State: open

When no `constructor` key is provided for a subclass, CoffeeScript generates the following code for its constructor:

```javascript
function Subclass() {
Subclass.__super__.constructor.apply(this, arguments);
}
```

I'd like it to generate this instead...

```javascript
function Subclass() {
return Subclass.__super__.constructor.apply(this, arguments);
}
```

Why? Well, I am writing a class that extends from `jQuery`. There are a couple critical points in jQuery's code where they call `this.constructor` without the `new` keyword assuming they will be calling `jQuery`, which constructs objects even when called without `new`. I don't know why they assume this, but they do. My base class constructor could detect this by setting a flag on the object, and just return the result of `jQuery` when called a subsequent time. But because subclasses class don't `return` the result of their call to the super constructor, the behavior won't carry over to subclasses.

You've called the ability to `return` a different object from a constructor a mis-feature, and perhaps it is. But it is how JavaScript works, and in this case I need to inter-operate with a library that assumes this behavior. Thanks!