google / traceur-compiler

Traceur is a JavaScript.next-to-JavaScript-of-today compiler
Apache License 2.0
8.18k stars 580 forks source link

Allowing this in super call. #1930

Open ever-null opened 9 years ago

ever-null commented 9 years ago

I am attempting to pass a function to the superclass and receiving the "this is not allowed before super()" error. Is it possible to have it not inspect this when it is wrapped in a delegate?

class Bar extends Foo {
  constructor() {
    super(() => this.doSomething());
  }

  doSomething() { }
}
caitp commented 9 years ago

this should be doable:

should work just fine, maybe

ever-null commented 9 years ago

I've also tested it without the lambda expecting the delegate to be called using fn.call(...) and this also raises the error:

class Bar extends Foo {
  constructor() {
    super(function() {
      // Expecting `this` to come from superclass call of fn.call(this, ...args).
      this.doSomething();
    });
  }

  doSomething() { }
}

I would think this scenario would be allowed given the context of this could come from anywhere then.

arv commented 9 years ago

Right now we only do static checking for this in constructors. We would have to combine that with a runtime check in these kind of situations.

On Sun, May 10, 2015 at 9:13 AM, hayesmg notifications@github.com wrote:

I've also tested it without the lambda expecting the delegate to be called using fn.call(...) and this also raises the error:

class Bar extends Foo { constructor() { super(function() { // Expecting this to come from superclass call of fn.call(this, ...args). this.doSomething(); }); }

doSomething() { } }

— Reply to this email directly or view it on GitHub https://github.com/google/traceur-compiler/issues/1930#issuecomment-100639684 .

erik

ever-null commented 9 years ago

@arv Thanks for the reply. I'm guessing this might be enough of an edge case that you guys won't consider implementing it? I suppose I could use 0.0.85 or introduce a method that is called after super that initializes the superclass' internal state.