Episode 6 : Understanding the this Keyword in JavaScript π§
Introduction
The this keyword in JavaScript behaves differently depending on where and how it is used. Its value is determined at runtime, based on the calling context --- not where the function is defined.
π 1. Global Scope
console.log(this);
// Output β window (in browser)
// Output β {} or global (in Node.js)
In browsers, the global object is the window. In Node.js, the global object is global. So, this in the global context refers to the global object. π§ Note: The global object differs across environments.
π§© 2. Function Scope
function x() {
console.log(this);
}
x();
// Output β window (in non-strict mode)
// Output β undefined (in strict mode)
π¨ Strict Mode and this
"use strict";
function x() {
console.log(this);
}
x();
// Output β undefined
In non-strict mode, this defaults to the global object (window). In strict mode, this remains undefined.
π 3. This Substitution Phenomenon JavaScript uses a behavior called "this substitution": When this inside a function is undefined (in non-strict mode), JS automatically substitutes it with the global object. Example:
function x() {
console.log(this);
}
x(); // β window (this substitution)
window.x(); // β window
π‘ When called as a reference (object.method()), this refers to the object before the dot.
π§± 4. this Inside Objects (Method Call) When a function is a property of an object, it is called a method.
const obj = {
a: 10,
x: function () {
console.log(this);
},
};
obj.x();
// Output β obj
β Inside an object method, this refers to the object itself. β οΈ However, if you extract the method and call it separately, this will no longer refer to the object.
const func = obj.x;
func();
// Output β window (non-strict) or undefined (strict)
5 call, apply, and bind Methods These are ways to explicitly control the value of this
-π§© call() Used to call a function with a specific this context and arguments passed individually.
function greet(greeting, name) {
console.log(`${greeting}, ${name}! I'm ${this.role}`);
}
const person = { role: "Developer" };
greet.call(person, "Hello", "Akshay");
// Output β Hello, Akshay! I'm Developer
π§© apply() Same as call(), but arguments are passed as an array.
greet.apply(person, ["Hi", "John"]);
// Output β Hi, John! I'm Developer
π§© bind() Returns a new function with this permanently bound to the provided object.
const boundGreet = greet.bind(person, "Hey", "Alice");
boundGreet();
// Output β Hey, Alice! I'm Developer
βοΈ Use bind() when you want to store a function with a fixed this for later execution.
β‘ 6. Arrow Functions and this Arrow functions don't have their own this. They lexically inherit it from their enclosing (parent) scope.
const obj = {
a: 42,
x: () => {
console.log(this);
},
};
obj.x();
// Output β window (not obj!)
Explanation: Arrow functions capture this from the surrounding scope at the time of creation. Here, this refers to the global scope, not obj.
β Correct Example with Regular Function
const obj = {
a: 42,
x: function () {
console.log(this);
},
};
obj.x();
// Output β obj
π‘ Enclosing Lexical Context (in Arrow Functions)
When we say that an arrow function inherits this from its enclosing lexical context, we mean:
Arrow functions donβt have their own this binding.
Instead, they capture (or close over) the this value from the nearest non-arrow (regular) function or execution context in which they are defined.
π§ 7. Summary Table
| Context | Value of this | Notes |
|---|---|---|
| Global Scope | window / global | Depends on environment |
| Function (non-strict) | window | this substitution |
| Function (strict) | undefined | No substitution |
| Method in object | The object | Depends on call site |
| Arrow Function | Lexical this | Inherited from parent |
| call, apply, bind | Explicit this | Manually controlled |
π― Key Takeaways
- β
thisdepends on how a function is called, not where itβs defined. - β‘ Arrow functions donβt have their own
thisβ they lexically inherit it from their parent scope. - π§© Use
call(),apply(), orbind()to manually control the value ofthis. - π Always enable
"use strict"to avoid unintendedthissubstitution.
πΊ Watch Live On YouTubeβ
