JS के IIFEs
In this page:
(function() {
// runs immediately
})();
(() => {
// runs immediately
})();
IIFE क्या है?
IIFE का मतलब है Immediately Invoked Function Expression। यह बनते ही चल जाता है। (function(){ ... })() में code को लपेटना इसे तुरंत एक बार execute करता है, बाद में कोई named function बचाए बिना।
उदाहरण: What Is an IIFE?
(function () {
// Print "Runs immediately" to the console
// Print "Runs immediately" to the console
console.log("Runs immediately");
})();
IIFEs क्यों इस्तेमाल करें?
एक IIFE अपना खुद का scope बनाता है। इसके अंदर के variables global variables नहीं बनते। ES modules के standard बनने से पहले, यह libraries के लिए अपने internal helper variables से global namespace को गंदा होने से बचाने का मुख्य तरीका था।
उदाहरण: Why Use IIFEs?
(function () {
var secret = "hidden"; // not a global variable
console.log(secret);
})();
console.log(typeof secret); // "undefined"
Return के साथ IIFE
एक IIFE तुरंत एक value return कर सकता है। उस returned value को एक variable में store करें। यह pattern, const result = (function(){ ... return value; })(), आपको local helper variables का इस्तेमाल करके एक value calculate करने देता है जो IIFE के बाहर leak नहीं होते।
उदाहरण: IIFE With Return
const result = (function () {
// Declare the constant `a`, set to `5, b = 10`
// Declare the constant `a`, set to `5, b = 10`
const a = 5, b = 10;
// Return `a + b` from this function
// Return `a + b` from this function
return a + b;
})();
// Print `result` to the console
// Print `result` to the console
console.log(result);
Parameters के साथ IIFE
एक IIFE एक normal function की तरह ही arguments स्वीकार कर सकता है। उदाहरण के लिए, (function(name){ console.log(name); })(Alice) एक regular function को कॉल करने जैसा व्यवहार करता है, बस definition के समय ठीक एक बार invoke होते हुए।
उदाहरण: IIFE With Parameters
(function (name) {
// Print `name` to the console
// Print `name` to the console
console.log(name);
})("Alice");
Modern इस्तेमाल
आज modules ज़्यादा आम हैं, पर IIFEs अभी भी उपयोगी हैं जब आपको किसी script में एक isolated scope चाहिए।
module-based codebases में भी, एक छोटा IIFE कभी-कभी इस्तेमाल किया जाता है जब आपको setup logic का एक self-contained block चाहिए जिसका internal हिस्सा दिखना नहीं चाहिए।
उदाहरण: Modern Use
const config = (function () {
// Declare the constant `secret`, set to "key123"
// Declare the constant `secret`, set to "key123"
const secret = "key123";
// Return `{ getKey: () => secret }` from this function
// Return `{ getKey: () => secret }` from this function
return { getKey: () => secret };
})();
// Print `config.getKey()` to the console
// Print `config.getKey()` to the console
console.log(config.getKey());
- wrapping parentheses भूल जाना, जैसे
function() {}(), जो एकSyntaxErrorहै। - trailing
()भूल जाना, इसलिए function define तो होता है लेकिन कभी नहीं चलता। - किसी IIFE से पहले वाली line पर semicolon छोड़ देना, इसलिए JavaScript दोनों lines को जोड़ देता है और एक error throw करता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: