Given the following CoffeeScript class:
class Foo
constructor: ()->
@bar()
bar: ->
$("body").on "click", (e)=>
@baz(this)
baz: (something)->
console.assert(something == $("body"))
The following Javascript is produced:
(function() {
var Foo;
Foo = (function() {
function Foo() {
this.bar();
}
Foo.prototype.bar = function() {
var _this = this;
return $("body").on("click", function(e) {
// Replaces both @ and this with _this
return _this.baz(_this);
});
};
Foo.prototype.baz = function(something) {
return console.assert(something === $("body"));
};
return Foo;
})();
}).call(this);
Shouldn't Coffeescript only replace @
with _this
inside the callback? I'd like for this
to be explicit and never treated magically, as is @
.
It would certainly make behavior easier to understand.
I'd like for it to refer to the HTML body element. So the compiled would be:
Coffeescript has
@
, why treatthis
magically as well?From what Jamon has said, CS compiles
@
intothis
as one of the first steps, so it'd be a fairly complex rewrite.