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

Stack Overflow Asked by zerkms on December 3, 2020

class Foo {
  static v = 123;

  static bar = () => this.v;


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 for another example.)

One Answer

tl;dr: Every class field (static or not) is internally wrapped in a method which gets invoked with the corresponding receiver (class or instance) at some point.

So, I'm not sure on some of those details *, but basically this happens:

For every field with an initializer (static or not), a function/method is created, with the initializer as its body. So this

static foo = () => this.v;

becomes something like this internally

function () { () => this.v }

That's in the proposal in step 28, which eventually leads to ClassFieldDefinitionEvaluation in this spec. The method is created in step 3.e.

The static fields (which are methods now) are then taken and called with the class object itself as receiver (i.e. the this value inside that intermediate method is set to the class object). This happens in step 34.a, which leads to DefineField in this spec. Finally the return value (in your case the arrow function) is used as value for the actual property.

Expressed as code, this is roughly what happens:

class Foo {}

Foo.v = function() { return 123; }.call(Foo); = function() { return () => this.v; }.call(Foo);

*: I'm not quite clear how the intermediate method returns the value, but there is probably something that says that the last expression of the function body is returned or something.

Correct answer by Felix Kling on December 3, 2020

Add your own answers!

Related Questions

Python, change a var by a string

4  Asked on February 21, 2021 by beardlongo


Vue – how to show an array of data in a table?

2  Asked on February 21, 2021 by jayk23


CMake unit testing – Unresolved external symbol

1  Asked on February 21, 2021 by chrispytoes


Break string into new rows after grepl

2  Asked on February 21, 2021 by ip2018


Send unicode sms with java

3  Asked on February 21, 2021 by hong4rc


Getting n1 n2 outputs after conversion

2  Asked on February 21, 2021 by icemilo


Is that possible to make for each user table in database

0  Asked on February 21, 2021 by haddadi-abdraouf


Run-time Error 1004; Error handling

2  Asked on February 20, 2021 by karl-drews


Password Generating program in python

2  Asked on February 20, 2021 by m-ismail


Error TS1086:An accessor cannot be declared in an ambient context

0  Asked on February 20, 2021 by leonardokunkel


Pass object data as FormData

2  Asked on February 20, 2021 by swarup-chavan


Iteration in Python throws results no desired

1  Asked on February 20, 2021 by catalina-hernndez


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir