jashkenas/coffee-script — Issue #1947: Wrong `this` value in nested fat-arrow function in class body
Author: shesek
Created: 2011-12-19 11:14:21+00:00
State: open

```coffee
class Foo
# `this` here refers to Foo, yet `_this` is set to `this` and not `Foo`

bar = =>
# One level of fat arrow function compiles `this` directly into `Foo`,
# as it does in class body. (making the `_this` variable pretty useless)
this is Foo

baz = => =>
# However, two levels (or more) of far arrow functions does use the _this
# variable, which contains a wrong value.
this is window # or undefined in ES5 strict
```

Either set `_this` to the regular `this` as compiled in class bodies (the constructor function and not the real `this`) or make `this` compile directly to the constructor function (as it does in `bar` above). IMO the second option is preferable, but probably harder to implement? Also, if the second option is chosen, get rid of the `_this` variable (which is added and not used in cases like `class Foo then =>`).

The use-case is pretty obvious and I its pretty clear its a bug, but if someone wonders that's what I was doing:

```coffee
class Foo
@bar: ->

$ ->
$('[data-op]').click => do @[$(this).data('op')]