A closure is the wrapping up of function and it's surrounding variables with in a function or lexical scope. In the other way we can say, we can access the outer function scope from the inner function. So we can say, closure are created when functions are created. Example of closure: function outerMethod() { var name = 'Hello World'; function innerMethod() { console.log(name); } innerMethod(); } outerMethod(); So output of the above example is: Hello World Here the name variable is in the scope of outer function and we are accessing from the inner function. This is closure. More understand about closure with it's lexical scoping: In JavaScript we can return a function within another function. Please check the below example. function outerMethod() { var name = 'Hello World'; ...
Comments
Post a Comment