Guest User

Untitled

a guest
Jun 17th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. // A closure is the combination of a function and the lexical environment within which that function was declared. This environment consists of any local variables that were in scope at the time the closure was created.
  2. // Whenever you return a function from another function which have a reference to a variable(s) of the outter function, you have created a closure. The closure close the referenced variables and don't let them be deleted from memory by garbage collection even after the execution of the outer function has been finished and their execution context popped off the execution stack.
  3. // Variables created in a function cannot be accessed outside the function. However, sometimes you need to access such variables. You can do so with the help of closures. With closusres you can use the outer function's variables at a later time.
  4. function a() {
  5. let b = 1;
  6. return function c() {
  7. return b;
  8. };
  9. }
  10. let d = a();
  11. console.log(d()); // 1
  12. console.dir(d);
Add Comment
Please, Sign In to add comment