This, call, apply and bind in JavaScript

Jida Asare
3 min readMar 10, 2020
Image of a laptop with JavaScript written beside it

The ever-present “this” keyword has been the source of confusion, buggy code, and frustration on the part of programmers. This post seeks to, among other things, demystify workings of the “this” keyword and reveal a better mental model to help eradicate any potential confusion surrounding it.

“This” Keyword

The “this” keyword refers to the object in the context of the current code(scope). It refers to the context object or better put the object executing the current function or statement.
Example 1

console.log(this) in the browser

Output 1

the output of logging “this” in the browser is the window object but in nodejs runtime, it is the global object

When the keyword “this” is logged, in the browser the output is the window object. In Nodejs runtime, however, the output is the global object. This is because the property “color” is within the scope of the window object.
Therefore window.color=’yellow window’ is the same as this.color=’yellow window’.

“This” in a defined object

Example 2 — “This” in a defined object
Let’s take a look at example 2.

console.log(this) in an object literal

Output 2

The output of console.log(this) in an object literal

In example 2, the object executing the function is the person object. This is because the method “favoriteMeal” is within the scope of the object “person”. Therefore this.name is similar to person.name.

Example 3
What do you think will be the output of the code below?

Logging “this” in a function which is in a strict mode

Output 3.i

window object

Output 3.ii

Error logging a value

The Output 3.ii will be logged in the console. The function has no context object.

Using the strict mode, the value of ‘this’ is undefined. To associate the function to an object the usage of the methods: call, apply or bind are employed. The three methods provide a context object for the function they are attached to. Even though they all seem to do the same function, the three are different in the way they are used.

CALL

Setting the context of a function using the call method

When the call method is used, the function is invoked immediately whiles passing the arguments one after the other.

Output

The output of setting the context of a function using the call method

APPLY

Setting the context of a function using the apply method

Output

The output of setting the context of a function using the apply method

When the “apply” method is used, the function is invoked immediately whiles passing the arguments as an array.

BIND

Setting the context of an object using the “Bind” method

Output

The output of setting the context of a function using the “bind” method

Whiles Call and Apply invokes the function and passes the arguments to it, bind initially sets the context of the function to the preferred object. The function’s invocation is deferred to a later time where the function is called.

I hope this explains “This”, “Call”, “Apply” and “Bind” in a way that it is better understood. Do leave comments, suggestions, etc below.

--

--