Same as this , where is the object initializer. So in both of your examples, this is the same as this , where is your line var a = ... this never changes within the specified execution context, and object initializers do not create a new execution context; only functions and eval do this. In your examples, the only time a new execution context is created, you call fn , and since fn is an arrow function, it closes above this in the execution context where it was created (which happens to be the global execution context in your examples).
The reason you see 100 for this.c in your code example is because this in the global scope (in free mode) refers to the global object, and var variables in the global scope become properties of the global object, and therefore this.c is a global variable c .
If you put all this code in a scope function, for example:
(function() {
... this.c will be undefined , because although this will still refer to the global object, c will no longer be a global variable (and therefore is a property of the global object).
If you want this inside fn to refer to b in the expression abfn() , then you do not need an arrow function, you need a normal function; you will get 10 ( abc value), not 5 ( ac value).
Of course, since this is a one-time object and fn closes over a , you can also just do fn body return ac; or return abc; depending on which c you want.
source share