क्लोज़र JavaScript में प्रोग्रामिंग का एक तरीका है, जिसमें किसी बाहरी लेक्सिकल स्कोप के वेरिएबलो को कोड के अंदर एक नेस्टेड ब्लॉक में इस्तेमाल किया जा सकता है। JavaScript में क्लोज़र बिना किसी अतिरिक्त काम के चलते हैं, और अक्सर लोग यह जाने बिना भी इन्हें इस्तेमाल कर लेते हैं कि ये असल में क्या हैं।
// Top-level declarations are global-scope
const dozen = 12;
// Functions create a new function-scope and block-scope.
// Referencing the outer variable here is a closure.
function nDozen(n) {
return dozen * n;
}
क्लोज़र नाम ऐतिहासिक रूप से λ-कैलकुलस से लिया गया है और इसे scheme ने लोकप्रिय बनाया (स्रोत); यह नाम किसी फंक्शन के खुले और बंद वेरिएबल बाइंडिंगों की ओर इशारा करता है।
डेटा प्राइवेसी / डेटा एनकैप्सुलेशन
// Consider this function:
const saveNumber = (number) => {
// The saved value is unaccessible by the outer lexical scope.
const value = number;
// We can provide access to the primitive value with a function, but the original will never change
return () => value;
};
// Attempting to set the variable outside of its lexical scope results in an error
value = 42;
पार्शियल एप्लीकेशन
// The arguments are applied one-by-one, using closures to arrive at the final result
function partialBuildUri(scheme) {
return function (domain) {
return function (path) {
return `${scheme}://${domain}/${path}`;
};
};
}
// A function could apply them all at once.
function buildUri(scheme, domain, path) {
return partialBuildUri(scheme)(domain)(path);
}
// Or apply a few to create a flexible system of functions for reuse.
function buildHttpsExercismUri(path) {
return partialBuildUri('https')('exercism.org');
}
किसी भी प्रोग्रामिंग भाषा में क्लोज़र का वेरिएबल स्कोप से गहरा संबंध होता है। JavaScript में वेरिएबलो के लिए तीन तरह के लेक्सिकल स्कोप होते हैं:
var कीवर्ड से घोषित किए जाते हैं।{...}) के अंदर let और const कीवर्ड से घोषित किए जाते हैं।व्यवहार में क्लोज़र तब बनता है जब कोई फंक्शन (या ब्लॉक) किसी बाहरी स्कोप का वेरिएबल इस्तेमाल करता है। JavaScript में यह अपने आप काम करता है, इसलिए अक्सर लोग बिना जाने ही क्लोज़र इस्तेमाल कर लेते हैं।
var कीवर्ड एक फंक्शन स्कोप वाला वेरिएबल बनाता है। इसका मतलब है कि var से बनाए गए वेरिएबल उस फंक्शन में कहीं भी इस्तेमाल किए जा सकते हैं जहाँ वे बनाए गए हैं, और उसके अंदर के हर लेक्सिकल स्कोप (फंक्शन हो या ब्लॉक) में भी।
{
var five = 5;
}
// This will succeed because 'five' exists in the same scope
var tenTimes = five * 10;
let और const कीवर्ड ब्लॉक स्कोप वाले वेरिएबल बनाते हैं। let ऐसा वेरिएबल बनाता है जिसे बदला जा सकता है, यानी उसकी वैल्यू कई बार रखी जा सकती है और उसे बिना शुरुआती वैल्यू दिए भी बनाया जा सकता है। const ऐसा वेरिएबल बनाता है जिसे बनाते समय ही उसकी शुरुआती वैल्यू देनी पड़ती है, और उसके बाद उसकी वैल्यू नहीं बदली जा सकती। अगर const वेरिएबल में कोई प्रिमिटिव वैल्यू रखी जाए, तो उसे बदला नहीं जा सकता। अगर const वेरिएबल में कोई रेफरेंस वैल्यू रखी जाए, जैसे कोई ऐरे या ऑब्जेक्ट, तो रेफरेंस तो नहीं बदलेगा, पर ऐरे के अंदर की चीज़ें बदली जा सकती हैं।
इस ब्लॉक को देखिए:
// 'two' is defined at the top-level, therefore available to the global-scope
const two = 2;
{
// This succeeds, as 'two' is available from the outer lexical scope
const oneLess = two - 1;
}
// This throws an error because oneLess is not defined outside of the previous inner lexical scope
console.log(oneLess);
हालाँकि हम ब्लॉक बनाने के लिए ब्रेसिज़ ({...}) का इस्तेमाल कहीं भी कर सकते हैं, पर अक्सर ये if स्टेटमेंट, फंक्शन और क्लास के आसपास ही दिखते हैं।
const PI = 3.14;
// This function uses a closure for the value of PI (π)
function areaOfCircle(radius) {
return PI * radius * radius;
}