jashkenas/coffee-script — Issue #2137: Custom class extends
Author: mazniak
Created: 2012-02-24 22:26:35+00:00
State: open

```coffeescript
class Foo
@__extends = (child, parent, ext) ->
# `ext` is coffeescript's __extends function
ext(child, parent)
console.log("#{child.name} extended #{parent.name}")

class Bar extends Foo
# => "Bar extended Foo"
```

Here is a possible solution to the missing extended hook. Why not give classes the ability to define their own 'extended' methods. It opens up all sorts of metaprogramming possibilities (like Ruby-esque hooks) and other cool stuff (e.g. psuedo-private/protected class properties).

Here's what the compiled Bar class looks like:

```javascript
Bar = (function(_super) {
(typeof _super.__extends === 'function' && _super.__extends(Bar, _super, __extends)) || __extends(Bar, _super);

Bar.name = 'Bar';

function Bar() {
return Bar.__super__.constructor.apply(this, arguments);
}

return Bar;
})(Foo);
```

As you can see, classes are extended normally when the `__extends` property is not defined on the class to be extended.

Quick implementation here: https://github.com/mazniak/coffee-script/compare/custom-extends-hook