JS के call apply bind
In this page:
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()
// 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()
// 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()
// 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
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
// 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();
- parentheses के बिना
fn.callलिखना याbind()के result को कॉल करना भूल जाना, क्योंकिbindएक नया function return करता है और उसे चलाता नहीं। apply()के बजायcall()को एक array pass करना, या इसका उल्टा, इसलिए arguments गलत shape में पहुंचते हैं।thisबदलने के लिए किसी arrow function परcallयाbindइस्तेमाल करना, जिसका कोई असर नहीं होता।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: