Sign up for Free !!
World's first open source developer community with
Ticketing System
Thanks in advance
A closure is an inner function that has access to the variables in the outer (enclosing) function’s scope chain. The closure has access to variables in three scopess:
var globalVar = "abc"; (function outerFunc(outerArg) { var outerVar = 'x'; (function innerFunc(innerArg) { var innerVar = 'y'; console.log( "outerArg = " + outerArg + "\n" + "innerArg = " + innerArg + "\n" + "outerVar = " + outerVar + "\n" + "innerVar = " + innerVar + "\n" + "globalVar = " + globalVar); })(123); })(456);
In the example, variables from innerFunc, outerFunc, and the global namespace are all in scope in the innerFunc. The output of the above code is
innerFunc
outerFunc
outerVar=x
innerVar=y
outerArg= 456
innerArg=123
globalVar=abc
Please Login/Register to write your answer !!!
A closure is an inner function that has access to the variables in the outer (enclosing) function’s scope chain. The closure has access to variables in three scopess:
In the example, variables from
innerFunc
,outerFunc
, and the global namespace are all in scope in theinnerFunc
. The output of the above code isouterVar=x
innerVar=y
outerArg= 456
innerArg=123
globalVar=abc