What are IIFE (Immediately Invoked Function Expressions)? IIFE क्या हैं?
An IIFE is a function that is defined and immediately executed at the same time. It's a design pattern that creates a new function scope to avoid polluting the global namespace.
// Basic IIFE
(function() {
console.log('This runs immediately!');
})();
// IIFE with return value
const result = (function() {
return 'Hello from IIFE';
})();
console.log(result); // 'Hello from IIFE'
// IIFE with parameters
(function(name, age) {
console.log(`Hello ${name}, you are ${age}`);
})('John', 30); // 'Hello John, you are 30'
// IIFE with arrow functions
(() => {
console.log('Arrow function IIFE');
})();
// Create private scope - avoid global namespace pollution
const myModule = (function() {
let private = 'secret'; // private variable
return {
getSecret: () => private,
setSecret: (newSecret) => { private = newSecret; },
publicMethod: () => console.log('Public method')
};
})();
console.log(myModule.private); // undefined - private is protected
console.log(myModule.getSecret()); // 'secret'
myModule.setSecret('new secret');
// IIFE with immediately invoked result
const add = (function() {
const a = 10;
const b = 20;
return a + b;
})();
console.log(add); // 30
// Module pattern using IIFE
const Calculator = (function() {
// Private variable
let result = 0;
// Private method
const log = (value) => console.log(value);
return {
add: (x) => { result += x; log(result); return this; },
subtract: (x) => { result -= x; log(result); return this; },
multiply: (x) => { result *= x; log(result); return this; },
getResult: () => result
};
})();
Calculator.add(5); // 5
Calculator.add(3); // 8
Calculator.multiply(2); // 16
console.log(Calculator.getResult()); // 16Interview tip: Explain that IIFE is especially useful for the module pattern to create private variables and methods. Before ES6 modules were popular, IIFE was the primary way to avoid global namespace pollution. Mention modern alternatives like ES6 modules, but emphasize that IIFEs are still relevant and used in many codebases.
IIFE एक function है जो define होते ही immediately execute हो जाता है। Design pattern है जो global namespace को pollute करने से बचाने के लिए नया scope बनाता है।
// Basic IIFE
(function() {
console.log('Immediately चलता है!');
})();
// Return value के साथ
const result = (function() {
return 'Hello from IIFE';
})();
console.log(result);
// Parameters के साथ
(function(name, age) {
console.log(`Hello ${name}, you are ${age}`);
})('John', 30);
// Arrow function से
(() => {
console.log('Arrow IIFE');
})();
// Private scope बनाना - module pattern
const myModule = (function() {
let private = 'secret'; // private variable
return {
getSecret: () => private,
setSecret: (newSecret) => { private = newSecret; }
};
})();
console.log(myModule.private); // undefined
console.log(myModule.getSecret()); // 'secret'
// Direct value return
const add = (function() {
const a = 10;
const b = 20;
return a + b;
})();
console.log(add); // 30इंटरव्यू टिप: समझाएं IIFE module pattern के लिए उपयोगी है private variables और methods बनाने के लिए। ES6 modules popular होने से पहले, IIFE ही primary तरीका था global namespace को pollute करने से बचाने के लिए।
Was this answer clear?