mishoo/UglifyJS — Issue #290: Absolutely decimates CoffeeScript classes
Author: khiltd
Created: 2011-12-28 21:56:56+00:00
State: open

A brief example:

```coffeescript
class Foo extends Object
callMe: () ->
console.log(@constructor.name)

class Bar extends Foo
callMe: () ->
super
console.log(@constructor.__super__.constructor.name)

foo = new Foo()
bar = new Bar()

foo.callMe()
bar.callMe()
```

Compiles into:
```javascript
(function() {
var Bar, Foo, bar, foo,
__hasProp = Object.prototype.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };

Foo = (function(_super) {

__extends(Foo, _super);

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

Foo.prototype.callMe = function() {
return console.log(this.constructor.name);
};

return Foo;

})(Object);

Bar = (function(_super) {

__extends(Bar, _super);

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

Bar.prototype.callMe = function() {
Bar.__super__.callMe.apply(this, arguments);
return console.log(this.constructor.__super__.constructor.name);
};

return Bar;

})(Foo);

foo = new Foo();

bar = new Bar();

foo.callMe();

bar.callMe();

}).call(this);
```

Which runs and outputs:

```
Foo
Bar
Foo
```
to the console.

After passing through Uglify (with -b for readability), all __super__ references are destroyed:

```javascript
((function() {
var a, b, c, d, e = Object.prototype.hasOwnProperty, f = function(a, b) {
function d() {
this.constructor = a;
}
for (var c in b) e.call(b, c) && (a[c] = b[c]);
return d.prototype = b.prototype, a.prototype = new d, a.__super__ = b.prototype, a;
};
b = function(a) {
function b() {
b.__super__.constructor.apply(this, arguments);
}
return f(b, a), b.prototype.callMe = function() {
return console.log(this.constructor.name);
}, b;
}(Object), a = function(a) {
function b() {
b.__super__.constructor.apply(this, arguments);
}
return f(b, a), b.prototype.callMe = function() {
return b.__super__.callMe.apply(this, arguments), console.log(this.constructor.__super__.constructor.name);
}, b;
}(b), d = new b, c = new a, d.callMe(), c.callMe();
})).call(this);
```
And the output is

```
b
b
b
```

Which is completely and totally wrong.