jashkenas/coffee-script — Issue #2132: Fat arrow breaks when passing to intermediate function
Author: martinstein
Created: 2012-02-22 13:34:53+00:00
State: open

Take the following simple test case:

```coffee
createFunc = (func) ->
func

class Person
constructor: (@fullname) ->

sayName: createFunc ->
console.log @fullname
```

With the current master tarball (from 2012/02/22) it compiles as expected:

```js
// Generated by CoffeeScript 1.2.1-pre
(function() {
var Person, createFunc;

createFunc = function(func) {
return func;
};

Person = (function() {

Person.name = 'Person';

function Person(fullname) {
this.fullname = fullname;
}

Person.prototype.sayName = createFunc(function() {
return console.log(this.fullname);
});

return Person;

})();

}).call(this);
```

Now, exchange the thin arrow in the `sayName` definition to a fat arrow like this:

```coffee
#...
sayName: createFunc =>
console.log @fullname
```

In the compiled code, instead of binding to `_this`, the `fullName`-reference now refers to the class object itself. This is unexpected and almost certainly not the intended behavior.

```js
// Generated by CoffeeScript 1.2.1-pre
(function() {
var Person, createFunc;

createFunc = function(func) {
return func;
};

Person = (function() {
var _this = this;

Person.name = 'Person';

function Person(fullname) {
this.fullname = fullname;
}

Person.prototype.sayName = createFunc(function() {
return console.log(Person.fullname);
});

return Person;

}).call(this);

}).call(this);
```