rainforestapp / decaf

Coffeescript to ES.next transpiler. Now maintained over at
https://github.com/juliankrispel/decaf
MIT License
106 stars 10 forks source link

Member functions are bound too late in the constructor #147

Closed arty-name closed 8 years ago

arty-name commented 8 years ago

Input:

class A
  constructor: () ->
    setTimeout @b
  b: =>
    console.log @c
  c: '!'

Output:

class A {
  constructor() {
    setTimeout(this.b);
    this.b = this.b.bind(this);
  }

  b() {
    return console.log(this.c);
  }
}

A.prototype.c = "!";

This code will produce an error because b() is passed to setTimeout before it is bound to the instance, so it will be invoked with window or undefined as this.