JS क्लोज़र्स
function outer() {
let outerVariable = value;
return function inner() {
return outerVariable;
};
}
const closure = outer();
Closure क्या है?
एक closure तब होता है जब कोई function अपने outer scope के variables को याद रखता है। inner function बाद में उन variables को इस्तेमाल कर सकता है।
यह इसलिए संभव है क्योंकि JavaScript functions अपने enclosing scope का एक live reference रखते हैं, उसका कोई snapshot नहीं, इसलिए वे variables तब तक बने रहते हैं जब तक closure बना रहता है।
उदाहरण: What Is a Closure?
function outer() {
const message = "Hello";
function inner() {
console.log(message); // remembers outer's variable
}
return inner;
}
const greet = outer();
greet();
Private Data
Closures data को private रख सकते हैं। closure के बाहर का code hidden variable को सीधे access नहीं कर सकता।
यह किसी counter जैसी चीज़ें बनाने का एक आम तरीका है जो सही ढंग से increment होता है भले ही कोई दूसरा code उसकी internal count को सीधे पढ़ या overwrite न कर सके।
उदाहरण: Private Data
function makeCounter() {
let count = 0; // private, not accessible outside
return function () {
count++;
return count;
};
}
const counter = makeCounter();
console.log(counter());
console.log(counter());
Parameters के साथ Closures
हर call एक अलग closure बना सकती है। stored values को साझा होने की ज़रूरत नहीं है। चूंकि हर function call को अपना खुद का execution context मिलता है, एक closure return करने वाला factory function independent, एक-दूसरे में दखल न देने वाले instances बनाता है।
उदाहरण: Closures With Parameters
function makeMultiplier(factor) {
return (n) => n * factor;
}
const double = makeMultiplier(2);
const triple = makeMultiplier(3);
console.log(double(5), triple(5)); // independent closures
Practical इस्तेमाल
Closures counters, factories, और ऐसे functions के लिए उपयोगी हैं जिन्हें याद रखी गई settings चाहिए।
एक closure जो किसी database connection या API key को capture करता है, उदाहरण के लिए, आपको हर call पर वह configuration दोबारा पास किए बिना एक साफ function API expose करने देता है।
उदाहरण: Practical Use
// Define the function `createApiClient` taking `apiKey`
// Define the function `createApiClient` taking `apiKey`
function createApiClient(apiKey) {
// Return `function fetchData() {` from this function
// Return `function fetchData() {` from this function
return function fetchData() {
// Print `"Using key:", apiKey` to the console
// Print `"Using key:", apiKey` to the console
console.log("Using key:", apiKey);
};
}
// Declare the constant `client`, set to `createApiClient("secret123")`
// Declare the constant `client`, set to `createApiClient("secret123")`
const client = createApiClient("secret123");
// Call `client()`
// Call `client()`
client();
Practice
ऐसे closures बनाने की कोशिश करें जो कोई number या नाम याद रखें। एक makeCounter() function लिखने की कोशिश करें जो increment/decrement functions return करे जो एक hidden counter variable साझा करते हों।
उदाहरण: Practice
// Define the function `makeCounter` with no parameters
// Define the function `makeCounter` with no parameters
function makeCounter() {
// Declare the variable `count`, set to `0`
// Declare the variable `count`, set to `0`
let count = 0;
// Return `{` from this function
// Return `{` from this function
return {
increment: () => ++count,
decrement: () => --count,
};
}
// Declare the constant `c`, set to `makeCounter()`
// Declare the constant `c`, set to `makeCounter()`
const c = makeCounter();
// Print `c.increment(), c.increment(), c.decrement()` to the console
// Print `c.increment(), c.increment(), c.decrement()` to the console
console.log(c.increment(), c.increment(), c.decrement());
- किसी
varloop में closures बनाना और उम्मीद करना कि हर एक अपना खुद का number रखेगा, जबकि वे सभी same variable share करते हैं;letइस्तेमाल करें। - किसी closure में बड़े objects रखना, जो memory को free होने से रोकता है।
- outer function को हर बार कॉल करना और state के persist होने की उम्मीद करना, जबकि हर call एक नया अलग closure बनाता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: