refers to the object it belongs to on runtime, depending upon its call-site (where it is called).
However, understanding what it would refer to in any given context, requires a slightly deeper understanding of some relevant concepts, which will be covered in this article.
Just to start with,
can have the following values depending upon where it is accessed :
1. By default :
refers to the global object.
2. Inside a function :
refers to the global object. In
3. Inside a method :
refers to the owner object. (A method is a function that belongs inside an object. In other words, it’s a function that’s an object’s property.)
4. In an event :
refers to the element on which the event was triggered.
5. Inside an Immediately Invoked Function Expression (IIFE) :
refers to the global object. In strict mode,
, just like any other function in a global context.
6. Inside a Fat-Arrow function : When a fat arrow
is used to define a function, it doesn’t create a new value for
, instead, it keeps referring to the same object it was referring to outside of the function.
This article hopes to give you an understanding of how these values are assigned to this, and how this knowledge can be utilized to suit our requirements.
As discussed in the last section, we got to know that this is a runtime-binding made for each function invocation, which entirely depends upon where exactly it was called.
This location in the code where the concerned function was called, is called the call-site. An understanding of determining the call-site is crucial towards understanding what this would be bound to, at any given point of the execution.
While finding the call-site is generally as simple as locating where a function was called from, it might not always be that clear because of certain coding patterns that might obscure it.
Hence, it’s important to think about the call-stack, the stack of functions that have been called to get us to the current stage of the execution which we’re concerned with.
Let us take a simple example to illustrate how a call-stack and call-site could be determined.
By following the chain of function calls in order, you can determine the call-stack and call-sites.
* Tip for determining call-stack
Utilize the built-in JS
provided with any modern browser’s developer tools.
In the execution of any JS code, you can set a breakpoint by using the keyword
, to stop the execution at that point in the browser.
Let’s say, we add a breakpoint when
stops the execution at the custom breakpoint, and the function call-stack at that point can be viewed on the right side.
In the image above, we can see that the execution was stopped at the point where we mentioned the
keyword, as soon as
is called. At this point, we will not observe any execution of code that comes after the
log, in this case).
Our primary point of interest right now, is the call-stack which is clearly illustrated on the right-hand side, same as we determined in the example above.
at the bottom of the stack, refers to the initial global call to
Now that we understand what a call-site and a call-stack is, we can learn about how a call-site determines what
will hold during execution.
There are four general rules which apply. First, let’s understand them independently, and then, their order of precedence when multiple rules can apply to the call-site.
This is the default catch-all rule, when none others apply. It comes from the most common case of a function invocation, which a standalone function call.
Let’s look at the below example.
declared in global scope is the same as declaring a property on the global object of the same name.
, the reference to this defaults to the global object. Hence, we would see the value of
mode is in effect globally or inside
object is not permitted default binding. In that case, we will see the error
TypeError : 'this' is 'undefined'
If the call-site has a context object (if a function is called through an owning or containing object, as its property), implicit binding applies.
The rule states that, when there is a context object for a function reference, it’s that object that should be used for its method calls’
Let’s look at a few examples to illustrate the different cases which can arise.
Since the object
is synonymous to
Let’s look at another example to see how only the top or last level of an Object property reference chain matters to the call-site for implicit
As we can see, the
value is still
. That’s because the call to
is bound to its direct caller,
, which serves as its
binding. In this context, the
Let’s look at a few more examples to show common cases where implicit binding can seem unexpected.
In this example, we have lost our implicit
in case of assigning
to a different variable
. Now, for
, this refers to the global object (default binding takes place). Hence, for the call,
Now, a more common and not-so-obvious way this loss of implicit binding can occur is when we pass a callback function. Consider the following example :
Once again, inside the callback function executor
, we are effectively passing a reference to
. Upon execution,
will be bound to the
object again (or throw a
mode is enabled), instead of
It’s quite common for function callbacks to lose their
binding. Another unexpected outcome can arise when the function we’ve passed our callback to, intentionally alters the
to point to the
that triggered the event.
You are not really in control of how your callback function reference will be executed. So far, you don’t have any way of controlling the call-site to assign the binding you intended. This is where explicit binding comes into play.
To resolve the unintended loss of
with implicit binding, we can explicitly set the value of
to a given object for a function call.
There are several in-built methods that can help us achieve explicit binding, like :
bind() is a method of the Function.prototype property. This means bind() can be used by every single function.
The bind() method creates a new function that, when called, has its
keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.
In other words, bind() returns a new function that is hardcoded to call the original function with the
context set as specified.
call() and apply() are also methods of the Function.prototype property, with similar but slightly different usage.
The call() method calls a function with a given
value and arguments provided individually.
Whereas, the apply() method calls a function with a given
value, and arguments provided as an array (or an array-like object).
with explicit binding by
allows us to force its
to be the
Also, a noteworthy aspect of the above example is that all instances of
will bind their respective
to the execution of
within them. Such an explicit binding is also called hard binding.
When a function is invoked with new in front of it, otherwise known as a constructor call, the following things are done automatically.
-linked. (Out of the scope of this article)
binding for that function call.
It should be clear that the default binding is the lowest priority rule of the four.
Let’s compare implicit binding, explicit binding, and new binding with each other.
As we saw, the explicit binding of
took precedence over its own implicit binding, as it did for the second case as well.
Hence, explicit binding is of higher precedence than implicit binding.
So, new binding is more precedent than implicit binding.
cannot be used together, so something like
is not allowed to test new binding directly against explicit binding. But, we can still use a hard binding to test the precedence of the two.
var fourthAttempt = new catchPokémon.call(firstAttempt);
is hard-bound against
did not change
, as we may have expected, but it remained
Instead, the hard-bound call to
is able to be overridden with
was applied, we got the newly created object back, which we named
, and we see that
indeed has the value
Thus, the newly created
is used, rather than the previously specified hard-binding for
is able to override hard-binding.
The primary reason for this behaviour is to create a function that essentially ignores the
hard-binding, and presets some or all of the function’s arguments.
We can summarize the rules for determining this from a function call’s call-site, in their order of precedence.
Here they are :
? If so, this is the newly constructed object (New binding). Example,
var attempt = new catchPokémon("Pidgey");
, even hidden inside a
hard-binding? If so, this is the explicitly specified object (Explicit binding). Example,
var attempt = catchPokémon.call("Pidgeotto");
var attempt = firstAttempt.catchPokémon("Pidgeot");
mode (Default binding).
binding for an executing function requires finding the direct call-site of that function.
Once examined, four rules can be applied to the call site, in this order of precedence.
? Use the newly constructed object.
? Use the specified object.
You Don’t Know JS: this and Object Prototypes, by Kyle Simpson.
Previously published at https://anmshpndy.com/how-well-do-you-know-this
Create your free account to unlock your custom reading experience.