Introduction Hi everyone my name is Himanshu Pandey and this article is related closures in javascript. Closures are one of the most powerful aspects of JavaScript, allowing a function to access da…
Source: Closures in javascript
Introduction Hi everyone my name is Himanshu Pandey and this article is related closures in javascript. Closures are one of the most powerful aspects of JavaScript, allowing a function to access da…
Source: Closures in javascript
Introduction Hi everyone my name is Himanshu Pandey and this article is related closures in javascript. Closures are one of the most powerful aspects of JavaScript, allowing a function to access da…
Source: Closures in javascript
Closures are a concept when child function defined inside parent function and child function access the variable that defined in parent function.
var globalScopeVar = "dotnetfunda.com"; // Self invoking function for parent or IIFE (function outerScopeFunction (outerScopeArg) { // begin of scope outerFunction // Variable declared in outerScopeFunction function scope var outerScopeFuncVar = 'x'; // Closure self-invoking function or IIFE (function innerScopeFunction (innerScopeArg) { // begin of scope innerScopeFunction // variable declared in innerScopeFunction function scope var innerScopeFuncVar = "y"; console.log( "outerScopeArg = " + outerScopeArg + "\n" + "outerFuncVar = " + outerScopeFuncVar + "\n" + "innerScopeArg = " + innerScopeArg + "\n" + "innerScopeFuncVar = " + innerScopeFuncVar + "\n" + "globalScopeVar = " + globalScopeVar); })(5); // Pass 12 as parameter })(7); // Pass 8 as parameter
//Output of above code is
outerScopeArg = 7 outerFuncVar = x innerScopeArg = 5 innerScopeFuncVar = y globalScopeVar = dotnetfunda.com