Explicit Binding (call, apply, bind) & 'this' in JavaScript
🟢 Beginner Level | JavaScript Core Syntax
📖 Definition
JavaScript provides three explicit methods—.call(), .apply(), and .bind()—to manually control and set the execution context (this value) of any function.
🌍 Multilingual Explanations
English
.call() invokes a function immediately with comma-separated arguments. .apply() accepts arguments as an array. .bind() returns a new bound function with locked this context.
Hindi
call() aur apply() function ko turant run karte hain naye this value ke saath. bind() ek naya bound function return karta hai baad mein call karne ke liye.
⚖️ Method Comparison Matrix
| Method | Execution | Argument Format | Returns |
|---|---|---|---|
.call(thisArg, arg1, arg2) |
Immediate | Comma-separated list | Result of function execution |
.apply(thisArg, [args]) |
Immediate | Array / Array-like list | Result of function execution |
.bind(thisArg, arg1) |
Deferred | Comma-separated list | A new bound function wrapper |
💡 Production Code Example
const user1 = { name: "Alice", role: "Admin" };
const user2 = { name: "Bob", role: "Developer" };
function introduce(greeting, location) {
console.log(`${greeting}, I am ${this.name} (${this.role}) from ${location}.`);
}
// 1. Using .call() - Immediate execution with comma args
introduce.call(user1, "Hello", "New York");
// 2. Using .apply() - Immediate execution with array args
introduce.apply(user2, ["Hi", "London"]);
// 3. Using .bind() - Method borrowing & bound function reference
const introduceBob = introduce.bind(user2, "Hey");
introduceBob("Tokyo"); // Bound function call
⚠️ Common Pitfalls
Losing `this` in Callbacks: Passing an object method like button.addEventListener('click', user.handleClick) loses object context unless you write user.handleClick.bind(user) or use an arrow wrapper.
🎯 Tiered Practice Exercises
- Beginner: Borrow a greeting function from one object and invoke it on another using
.call(). - Intermediate: Use
.apply()withMath.max()to find the maximum value in an array. - Advanced: Create a partial application function wrapper using
.bind().
🧭 Navigation
← 05. Function Scope, Closures & IIFE | Functions Hub | Module Hub →