jashkenas/coffee-script — Issue #1913: Adding re-open class functionality?
Author: viatropos
Created: 2011-12-05 19:11:02+00:00
State: open

If I could do this, just like in Ruby, that would be awesome:

``` coffeescript
# app/models/article.coffee
class Article
title: -> "Article Title!"

# vendor/articles/article.coffee
class Article
createdAt: -> @_createdAt ||= new Date()
```

``` coffeescript
article = new Article
article.title() #=> "Article Title!
article.createdAt() #=> Date
```

The generated code would just check if it already exists:

``` javascript
Article = (function() {
if (!Article) {
function Article() {

}
}
//...
})();
```

There's probably several other considerations, such as 1. the above would only work in the global namespace, so you'd have to have namespace/module support otherwise. But, I thought I'd add it to the list for reference and as a possibility to see what you guys think. You can do the same with `_.extend MyClass, MyModule`, but it's just cleaner like the above.