Tuesday 22 September 2020

Why are arrow functions as static members values not lexically scoped?

class Foo {
  static v = 123;

  static bar = () => this.v;
}

console.log(Foo.bar());

I expect this code to return undefined, because arrow functions are lexically scoped, hence this must be eagerly bound to the outer scope.

Yet, it returns 123.

Why exactly does this happen?

And yep, I understand it's still stage 3, but still - why does the proposed standard behave like that? (See https://babeljs.io/docs/en/babel-plugin-transform-class-properties for another example.)



from Why are arrow functions as static members values not lexically scoped?

No comments:

Post a Comment