What is `this` inside foo.bar()?

The problem

Code showing - method which is passed as callback loses reference to original obj as this, but calling foo.bar() directly works

Calling a method directly works as expected.
But when we pass a method as callback, it loses reference to the original object (as this) when called.

Why is that? Can the spec help us explain this difference?

The explanation

For methods,

⭐️ foo.​bar() translates to foo.bar.call(foo).

That is, when a method (function which is accessed as a object property) is called, the object gets passed as this inside the function.

So, in this case -
What → foo is passed as this inside bar
When → if bar is a function and accessed as object property (i.e. foo.bar).

(Note - We are using the Function.call method above to estimate what the language is doing internally, by passing a custom this value)

For functions,

⭐️ fn() translates to fn.​call(undefined)

In case of a normal function call - this value within the function will be undefined.

But, there is a slight catch here. In case of non-strict mode, if this is set to undefined or null (as above), then it is internally replaced with the global object.

Effectively -

modethis value
non-strictglobal object

Method to function,

⭐️ Now, if we were to rewrite the method call as a function call, then the value of this will change from foo (object) to either undefined or global.

One example of this is rewriting foo.bar() using a intermediate variable - fn = foo.​bar; fn().

So, if bar was referencing other values from foo using this, those values will become undefined or resolve to a wrong variable.

🧠 This is exactly the reason why passing methods as callback changes the value of this (passed within it). Instead of calling a method directly, callback is actually passed as a function and this function is later called by some other code.

In other words,

⭐️ When foo.bar is called, the function bar is not aware that it is "attached" to the object foo. Based on the exact syntax of a function call, if the language can figure out a clear someThing.someFunction() structure, then it will happily forward someThing as this.

But, if you take out a function from a object and call it separately, there is no way to figure out which object it was originally attached to. Hence, this will be undefined.

📖 What does the spec say?

TLDR - If you would like to see me actually go through the spec, this video might be more interesting.

Part 1 of the video covers previous sections.

EvaluateCall- if IsPropertyReference, set this to base object

IsPropertyReference- reference where base is object or primitive


This foo.bar structure is defined in the spec as a PropertyReference.
If you were assigning a value to the property of a object or primitive, then anything that is valid as the Left Hand side of the assignment expression is a PropertyReference.

So, PropertyReference = Reference + base is object or primitive

PropertyReference illustration

Some examples of valid and invalid PropertyReference might make it more clear - 👇


  1. If it is a PropertyReference, then set thisValue to base of the PropertyReference (i.e. foo in foo.bar).
  2. Or else, thisValue is undefined.

The obtained thisValue is forwarded to the abstract operation Call. Call operation verifies that the resolved value of foo.bar is a function and then calls the internal method function.[[Call]] with the same thisValue.
This function.[[call]] internal method is very similar to the public method function.call, which we use to call a function with a custom thisValue.

The implementation of this function.[[call]] method is different for different type of callables.
Rough speaking, there are 4 type of callables (or simply, functions) -

  1. Function declaration and expression
  2. Arrow function
  3. Bound function
  4. Proxy, which supports [[call]]

Till now, we have talked about plain functions (type 1), which are defined using the function keyword. Now, let's look at arrow function and bound function. We'll skip proxies for this discussion.

Bound function and Arrow function -

The function foo.bar doesn't need to be a simple function object, but it can be anything with a [[call]] interface like a exotic bound function or a proxy.

Bound functions ignore the thisValue that was passed in and instead uses internal [[BoundThis]] as the actual this. [[BoundThis]] is the custom thisValue that was passed while binding using Function.bind.
That is why one solution to this callback problem is to bind a function to the object before passing it as a callback.

Note - Bound functions are called as exotic objects, because they don't follow the normal conventions of a object.

Arrow function is a type of function object whose this value is resolved from the lexical scope.
Its [[call]] method ignores the received thisValue (from Call abstract method) and always resolves this from its lexical scope, like any other free variable in a closure.

thisMode in function objects 👇 thisMode in function objects

Arrow function is a ordinary function object with internal [[ThisMode]] value set to lexical.

This can be another solution to the callback problem - creating a arrow function inside the object constructor will ensure that this always resolves to the base object.

🎬 That's all! Hope you had a interesting read.
I would really appreciate if you leave some feedback 🌀

Comments (1)

Aquib Vadsaria's photo

Wonderful well written article, Beautifully mapped/explained the ES specs to understand the JS concepts Thanks for sharing :)