A Closer Look at the 'this' keyword in JavaScript
- #javascript
Explore the 'this' keyword in JavaScript: its behavior in global scope, functions, methods, and callbacks, with examples
The this
keyword is a unique identifier in JavaScript. The value of this
is determined at runtime and can vary depending where you are using it. As a result, the behavior of the this
keyword seems confusing to many people, especially to those who are new to the language or have limited experience with its nuances.
In other languages, e.g. C++, Java, C# etc, the
this
keyword represents the current instance of a class, But JavaScript'sthis
keyword is a bit different in nature. So it's better not to compare with how it works in other languages.
This article aims to explore the value of the this
keyword in different contexts. Let's dive in and unveil the mysteries of the this
keyword!
this in global scope
Global object
When we use the this
keyword in the global scope (i.e. outside any function), it's value refers to the global
object.
The global
object is the top-level object in the scope chain and acts as a container for all global variables (defined with the var
keyword), functions, and objects.
In the browser environment, the global object is the window
object. It provides access to various properties and methods related to the browser environment.
Global functions like console.log
, parseInt
, setTimeout
or global objects like document
, Date
etc. are properties of the window
object.
In Node.js environment, the global object is global
object. It sounds strange as a sentence, but the identifier of the object itself is global
. It's similar to how the window
object is used in browsers but designed for server-side JavaScript execution.
this inside Functions
When a function is called, the value of the this
keyword refers to the global
object in non-strict mode, or undefined
in strict mode.
The same piece of code when run in
strict mode
, It will throw an error “Cannot read property ‘name’ of undefined”. It’s because the value ofthis
inside the function becomesundefined
instead of referring to theglobal
object.
Explicit function binding
Sometimes we need to explicitly bind the this
keyword to a specific object to ensure that the function operates with the correct this
value, regardless of how it is invoked.
We have three methods in JavaScript to accomplish this: call
, apply
, and bind
, which we may call on any function. When a function is invoked with the call
, apply
, or bind
, the this
keyword will refer to what we pass in as the first parameter.
Function.prototype.call()
In the following example, we’re explicitly binding the this
keyword to the person
object. It takes the following input parameters:
thisArg
: The value to be passed as the this context within the function.arg1
,arg2
, ...: Optional arguments to be passed to the function individually.
In this example, the sayHello
function is called with the call
method, which binds the this
to the person
object, so the output is "Hi, my name is Naimul".
Function.prototype.apply()
The apply
method is another method that allows us to call a function and explicitly set the this
value. However, unlike call
, apply
takes arguments as an array or an array-like object. It takes the following input parameters:
thisArg
: The value to be passed as the this context within the function.[arg1, arg2, ...]
: An array containing the arguments to be passed to the function.
Function.prototype.bind()
Unlike call
and apply
, bind
does not execute the function immediately but instead returns a new function where the this
context is fixed to a specified object.
Note that the
greet
parameter is pre-filled with "Hello". Even if we invoke it with a different greet value,sayHelloWithPerson("Hello")
, it won’t use the new value.
this inside methods
When a method is called on an object in JavaScript, the this
keyword inside that method refers to the object itself. This behavior is known as method binding and it's a fundamental aspect of how objects works in JavaScript. Let's look at the following example
What happens when we use arrow functions instead of regular functions? Let’s find out!
Arrow functions behave differently than regular functions. The arrow function printName
captures the this
value from the surrounding scope, which, in this case, is the global scope (the outermost scope).
It’s generally recommended to use regular function expressions or function statements. It’s because you need to access the object’s properties or other methods.
Nested function
What happens if we have nested functions within a method? Let’s move the console.log
in a nested function and call the function within the method. Trust me! We'll find out something very interesting.
The value of this
in the showGreetingMessage()
function is the global
object, because it is being invoked as a standalone function and not as a method of any object. It does not inherit the this
value of the outer scope. To fix this issue, we can take several approach
- Storing the
this
variable in aself
variable, and useself
inside the function - Calling the
showGreetingMessage
function withcall
,bind
orapply
- Switching to arrow functions
To fix this, we’ll convert our regular functions into arrow functions.
By switching from regular functions to arrow functions, the this
value is no longer redefined within the function, and instead, it inherits the this
value from the surrounding myFriend object. This allows the arrow functions to access the correct this
value, which is the myFriend object, and fix the issue with this
not pointing to the right object.
The topic of arrow functions is broad and can require its own in-depth exploration. In a future article, we can dive deeper into advanced concepts related to arrow functions and best practices for their usage in different scenarios.
Constructor functions
When a function is called as a constructor with the new
keyword, this
is bound to the newly created object. For example:
this inside callback functions
Callback functions are functions that you pass to another function as an argument, so that they can be executed after some async operation (most of the time, but not always). Let’s try to assume what will be the output of the following function
To fix this you can either explicitly bind the function to the proper this
value or you can use arrow functions.
In conclusion, I hope this article has shed some light on the this
keyword and now you understand it clearly in various contexts.