Skip to main content

Posts

Showing posts from October, 2018

JavaScript Closure

What is closure? It is my personal experience, I have never understood closure, I have read many articles & have seen many videos. Finally, I understood sometimes we even used a closure without realizing we were using a closure. First, understand the meaning of closure “an act or process of closing something”, now we can relate closure in javascript, closure is an inner function which wraps inside the outer function, which can access outer function scope & global function scope. for scoping you can read my previous blog:- http://uisource.blogspot.com/2018/08/javascript-variable-scoping.html why do we create? Global variables can be made local (private) with closures. To avoid the usage of global variables, use the local variables and wrap your code in the closure. A closure is an inner function that has access to the outer (enclosing) function’s variables—scope chain(Lexical scoping). The closure has three scope chains: it has access to its own scope (variables define