jashkenas/coffee-script — Issue #1988: Defining constructors as bound methods
Author: shesek
Created: 2011-12-30 04:16:05+00:00
State: open

First of all, I'm not entirely +1 on this, as it makes bound constructors inconsistent with other bound methods... that being said, maybe bound constructors could be bound the constructor function itself, so it can easily refer to ("static") properties of the constructor function?

I have a few classed that are purely "static", without any prototype properties, which also use the constructor to receive "messages" and delegate them to other methods (its really more of a function with some internal functions that it delegates to than it is a class, but classes are a much nicer way to organize that - its more readable as it stands out as a "special" block of code, it has a nicer syntax and I get a scope for private data). To write those, I'd like to be able to do:

```coffeescript
class foo
constructor: (req) =>
if req.bar is 'baz'
@qux req
else
@corge req
@qux: -> ...
@corge: -> ...
```

Which would compile `@` into `Foo` inside the constructor function. Currently, I have the class name written explicitly all over the constructor function, which is somewhat annoying after getting used to CoffeeScript's `@`.

As I mentioned at the beginning, this is inconsistent with "regular" bound prototype methods, as they're bound to the instance. However, it is consistent with the behavior of regular (non-prototype) bound functions, which also compile `this` to the constructor function.

Thoughts?