๐ฅ THE ULTIMATE JS LESSON โ this Keyword (Browser & Node)
Greetings, fellow developers! Today, we dive into a comprehensive exploration of the this keyword and the powerful call / apply / bind methods.
๐ Repo: javascript-complete-mastery
๐ 1. What is this in JavaScript?
this depends entirely on HOW a function is called, not where it is written.
๐ง 2. 5 Core Rules of this (Browser + Node)
Rule 1 โ Global Context
Browser
console.log(this);
๐ window
Node
console.log(this);
๐ {}
Rule 2 โ Function Context
Non-strict mode
function test() {
console.log(this);
}
test();
๐ Browser โ window
๐ Node โ global
Strict mode
"use strict";
function test() {
console.log(this);
}
test();
๐ undefined
Rule 3 โ Method Call (Object Method)
this = object before dot
const user = {
name: "Usman",
greet() {
console.log(this.name);
}
};
user.greet();
โ "Usman"
Rule 4 โ Constructor / Class
function Person(name) {
this.name = name;
}
const p = new Person("Usman");
console.log(p.name);
๐ this refers to the new object
Rule 5 โ Arrow Functions (NO own this)
const obj = {
value: 10,
test: () => {
console.log(this);
}
};
obj.test();
๐ Browser: window
๐ Node: {}
๐ฅ Browser vs Node Quick Comparison
| Scenario | Browser | Node |
|---|---|---|
| Global context | window |
{} |
| Function (non strict) | window |
global |
| Function (strict) | undefined |
undefined |
| Method | Object | Object |
| Arrow | Parent scope | Parent scope |
โก 3. Deep Examples (Tricky)
Example โ Losing this
const user = {
name: "Usman",
greet() {
console.log(this.name);
}
};
const x = user.greet;
x();
โ undefined
Example โ Fix with bind
const x = user.greet.bind(user);
x();
โ "Usman"
Arrow inside methods
const user = {
name: "Usman",
greet: function () {
setTimeout(() => {
console.log(this.name);
}, 100);
}
};
user.greet();
โ "Usman"
Arrow in object (incorrect assumption)
const obj = {
a: 10,
print: () => console.log(this.a)
};
obj.print();
โ undefined
๐งฉ Event Listeners
Normal function
button.addEventListener("click", function() {
console.log(this);
});
๐ this = button element
Arrow function
button.addEventListener("click", () => {
console.log(this);
});
๐ this = window
๐ฆ Node.js Module Wrapper
(function(exports, require, module, __filename, __dirname) {
// code
})();
So:
console.log(this); // {}
๐ Summary Cheat Sheet
this = depends on how the function is called
Browser
- Global โ window
- Function non strict โ window
- Strict โ undefined
- Method โ object
- Arrow โ parent
Node
- Global โ {}
- Function non strict โ global
- Strict โ undefined
- Method โ object
- Arrow โ parent
๐๏ธโโ๏ธ PRACTICE SECTION
โ EASY (3)
Problem 1
console.log(this);
โ Browser โ window
โ Node โ {}
Problem 2
function test() {
console.log(this);
}
test();
โ Browser โ window
โ Node โ global
Problem 3
const obj = {
a: 5,
show() {
console.log(this.a);
}
};
obj.show();
โ 5
โก MEDIUM (3)
Problem 1
const obj = {
a: 10,
b: () => console.log(this.a)
};
obj.b();
โ undefined
Problem 2
"use strict";
function x() {
console.log(this);
}
x();
โ undefined
Problem 3
const user = {
name: "Ali",
greet() {
setTimeout(function() {
console.log(this.name);
}, 0);
}
};
user.greet();
โ undefined
๐ฅ HARD (4)
Problem 1
const obj = {
name: "Usman",
greet() {
return () => console.log(this.name);
}
};
const x = obj.greet();
x();
โ "Usman"
Problem 2
class A {
constructor() {
this.num = 100;
}
show() {
console.log(this.num);
}
}
const a = new A();
const s = a.show;
s();
โ undefined
Problem 3
const obj = {
x: 15,
y: {
z: function() {
console.log(this.x);
}
}
};
obj.y.z();
โ undefined
Problem 4
let a = {
value: 50,
print: function() {
(function() {
console.log(this.value);
})();
}
};
a.print();
โ undefined
๐ THE ULTIMATE JS LESSON โ call(), apply(), bind()
JavaScript gives us three powerful methods to manually control the value of this inside a function.
These are essential for interview questions, real-world bug fixing, and writing professional code.
๐ฅ 1. Why do we need call/apply/bind?
Because sometimes, a function loses its this:
function greet() {
console.log(this.name);
}
const user = { name: "Usman" };
greet(); // โ undefined
We want to run greet with this = user.
Thatโs where these methods come in.
๐ง 2. call() โ calls the function immediately
func.call(thisValue, arg1, arg2, ...)
Example
function greet(age) {
console.log(this.name, age);
}
const user = { name: "Usman" };
greet.call(user, 22);
โ Sets this = user
โ Executes immediately
โ Arguments passed one-by-one
๐ง 3. apply() โ same as call, but arguments in an array
func.apply(thisValue, [arg1, arg2, ...])
Example
greet.apply(user, [22]);
โ Same output
โ Just a different argument format
๐ง 4. bind() โ returns a NEW function with fixed this
const newFn = func.bind(thisValue, arg1, arg2);
Example
const greetUser = greet.bind(user, 22);
greetUser();
โ Does NOT run immediately
โ Creates a new function with permanent this
๐ 5. The Three in One Line
call โ call immediately (args individually)
apply โ call immediately (args in array)
bind โ return new function (does NOT run)
๐ฆ 6. Real-world Uses
โ Borrowing methods
let obj1 = { name: "Ali" };
let obj2 = { name: "Usman" };
function sayHi() {
console.log("Hi " + this.name);
}
sayHi.call(obj2);
โ Using Array methods on non-arrays
const nums = {
0: 4,
1: 8,
length: 2
};
console.log(Array.prototype.join.call(nums, "-"));
โ Fixing this in event handlers
button.addEventListener("click", obj.method.bind(obj));
โ Currying with bind
function multiply(a, b) {
return a * b;
}
const double = multiply.bind(null, 2);
console.log(double(5)); // 10
๐งจ 7. Tricky & Important Behaviors
โ Arrow functions IGNORE call/apply/bind
const obj = { a: 10 };
const x = () => console.log(this.a);
x.call(obj); // โ still not obj
Arrow functions use lexical this, so you cannot change it.
โ bind creates a new function (original stays unchanged)
function a() {}
const b = a.bind({});
console.log(a === b); // false
โ Multiple binds โ first one wins
const x = greet.bind(obj1).bind(obj2);
x(); // uses obj1
โก 8. Browser vs Node?
No difference in behavior.
call/apply/bind work the same in both.
Only the default this differs, not these methods.
๐งฉ 9. Practice Problems
โ EASY (3 Problems)
Problem 1 โ Output?
function say() {
console.log(this.x);
}
const obj = { x: 10 };
say.call(obj);
Solution
10
Problem 2 โ Output?
function sum(a, b) {
console.log(a + b);
}
sum.apply(null, [5, 7]);
Solution
12
Problem 3 โ Output?
function greet(name) {
console.log("Hi " + name);
}
const g = greet.bind(null, "Usman");
g();
Solution
Hi Usman
โก MEDIUM (3 Problems)
Problem 1 โ Output?
const user = {
name: "Ali",
say() {
console.log(this.name);
}
};
const fn = user.say;
fn.call(user);
Solution
Ali
Problem 2 โ Output?
function add(a, b) {
console.log(this.x + a + b);
}
const obj = { x: 5 };
add.call(obj, 10, 15);
Solution
30
Problem 3 โ Output?
function intro(age, country) {
console.log(this.name, age, country);
}
const p = { name: "Usman" };
intro.apply(p, [22, "PK"]);
Solution
Usman 22 PK
๐ฅ HARD (4 Problems)
Problem 1 โ Output?
const a = { value: 50 };
function print() {
console.log(this.value);
}
const b = print.bind(a);
b.call({ value: 100 });
Solution
50
Problem 2 โ Output?
const obj = {
x: 10,
y: function() {
console.log(this.x);
}
};
const z = obj.y.bind({ x: 99 });
z();
Solution
99
Problem 3 โ Output?
function show() {
console.log(this.a);
}
const o1 = { a: 1 };
const o2 = { a: 2 };
const f = show.bind(o1);
f.call(o2);
Solution
1
Problem 4 โ Output?
function multiply(a, b, c) {
console.log(a * b * c);
}
const f = multiply.bind(null, 2);
f.apply(null, [3, 4]);
Solution
24
Congratulations on mastering this, call(), apply(), and bind()! To further enhance your JavaScript journey, Iโve developed a free, structured repository featuring topic-by-topic training, ranging from beginner to advanced levels, organized into a 42-day mastery plan. Completing this resource will significantly ease your transition into frameworks and stacks like React, Node, and Next.js.
๐javascript-complete-mastery
Top comments (0)