← Back to JavaScript Course | Chapter 6: Functions Advanced | Lesson 4 of 8

JS के call apply bind

call, apply, और bind आपको यह चुनने देते हैं कि किसी function के चलने पर this का क्या मतलब है, जैसे किसी दूसरे व्यक्ति को कोई tool उधार देना। call और apply इसे तुरंत चलाते हैं, जबकि bind एक नई ready-to-use version बनाता है।
Syntax
javascript
functionName.call(thisValue, arg1, arg2);
functionName.apply(thisValue, [arg1, arg2]);
const bound = functionName.bind(thisValue);

call()

call() किसी function को तुरंत चलाता है और आपको उसकी this value सेट करने देता है। Arguments एक-एक करके पास किए जाते हैं।

यह call() को तब सुविधाजनक बनाता है जब आपको पहले से पता हो कि किसी function को कितने arguments चाहिए और आप उसे तुरंत एक खास this value के साथ invoke करना चाहते हों।

उदाहरण: call()

javascript
// Define the function `greet` taking `greeting`
// Define the function `greet` taking `greeting`
function greet(greeting) {
  // Print `greeting + ", " + this.name` to the console
  // Print `greeting + ", " + this.name` to the console
  console.log(greeting + ", " + this.name);
}
// Declare the constant `user`, set to `{ name: "Sam" }`
// Declare the constant `user`, set to `{ name: "Sam" }`
const user = { name: "Sam" };
// Call `greet.call(user, "Hello")`
// Call `greet.call(user, "Hello")`
greet.call(user, "Hello");

apply()

apply() call() जैसा ही है, पर यह function arguments को एक array में स्वीकार करता है। apply() खासतौर पर तब उपयोगी है जब आपके पास arguments पहले से किसी array (या array-like object) में इकट्ठे हों और आप उन्हें manually spread नहीं करना चाहते।

उदाहरण: apply()

javascript
// Define the function `greet` taking `greeting`
// Define the function `greet` taking `greeting`
function greet(greeting) {
  // Print `greeting + ", " + this.name` to the console
  // Print `greeting + ", " + this.name` to the console
  console.log(greeting + ", " + this.name);
}
// Declare the constant `user`, set to `{ name: "Sam" }`
// Declare the constant `user`, set to `{ name: "Sam" }`
const user = { name: "Sam" };
// Call `greet.apply(user, ["Hi"])`
// Call `greet.apply(user, ["Hi"])`
greet.apply(user, ["Hi"]);

bind()

bind() एक fixed this value के साथ एक नया function बनाता है।

नए function को बाद में कॉल किया जा सकता है। bind() आमतौर पर किसी ऐसे callback के लिए this को lock करने में इस्तेमाल होता है जिसे बाद में दूसरे code द्वारा invoke किया जाएगा, जैसे कोई event handler जिसे किसी खास object instance को refer करना है।

उदाहरण: bind()

javascript
// Define the function `greet` with no parameters
// Define the function `greet` with no parameters
function greet() {
  // Print `"Hello, " + this.name` to the console
  // Print `"Hello, " + this.name` to the console
  console.log("Hello, " + this.name);
}
// Declare the constant `user`, set to `{ name: "Sam" }`
// Declare the constant `user`, set to `{ name: "Sam" }`
const user = { name: "Sam" };
// Declare the constant `boundGreet`, set to `greet.bind(user)`
// Declare the constant `boundGreet`, set to `greet.bind(user)`
const boundGreet = greet.bind(user);
// Call `boundGreet()`
// Call `boundGreet()`
boundGreet();

मुख्य Difference

call() और apply() अभी चलते हैं। bind() बाद के इस्तेमाल के लिए एक नया function return करता है। चूंकि apply() एक array स्वीकार करता है, यह variable-length argument lists के साथ भी अच्छे से काम करता है जहाँ आपको पहले से नहीं पता कि आप कितनी values पास कर रहे हैं।

उदाहरण: Main Difference

javascript
function sum(a, b) { return a + b; }
console.log(sum.call(null, 1, 2));   // runs now
console.log(sum.apply(null, [1, 2])); // runs now, args in array
const later = sum.bind(null, 1, 2);
console.log(later());               // runs later

Practice

जब arguments अलग-अलग हों तो call इस्तेमाल करें, जब arguments किसी array में हों तो apply, और जब आपको एक reusable function चाहिए तो bind।

एक आम pattern है const boundFn = obj.method.bind(obj) ताकि boundFn को obj से अपना connection खोए बिना बेझिझक इधर-उधर पास किया जा सके।

उदाहरण: Practice

javascript
// Declare the constant `obj`, set to `{ value: 42 }`
// Declare the constant `obj`, set to `{ value: 42 }`
const obj = { value: 42 };
// Define the function `show` with no parameters
// Define the function `show` with no parameters
function show() { console.log(this.value); }
// Declare the constant `boundFn`, set to `show.bind(obj)`
// Declare the constant `boundFn`, set to `show.bind(obj)`
const boundFn = show.bind(obj);
// Call `boundFn()`
// Call `boundFn()`
boundFn();
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. parentheses के बिना fn.call लिखना या bind() के result को कॉल करना भूल जाना, क्योंकि bind एक नया function return करता है और उसे चलाता नहीं।
  2. apply() के बजाय call() को एक array pass करना, या इसका उल्टा, इसलिए arguments गलत shape में पहुंचते हैं।
  3. this बदलने के लिए किसी arrow function पर call या bind इस्तेमाल करना, जिसका कोई असर नहीं होता।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.