Voice of a Developer: JavaScript Closures - Part Fourteen

JavaScript is a language of Web. This series of articles will talk about my observations learned during my decade of software development experience with JavaScript.

Before moving further let us look at the previous articles of the series:

In an aspiration to write better code, JavaScript provides closure. We’ll understand it now, but before that I'll shift gears to a few other concepts like scope & callback

Scoping

In programming, scoping is the part of a computer program where you bind a variable and its visibility. There are two types of scoping, i.e. lexical and dynamic. JavaScript is based on lexical or static scoping. The functions are lexically rather than dynamically scoped. JavaScript blocks scopes using {}, and a new scope is created when you create a new function.

A quick glance at another concept called callback:

Callback

A callback is basically a function that accepts another function as a parameter, I hope it recalls your memory of higher order functions. At some point, the higher order function can call the function passed as parameter, this is a callback.

The common use of closure we are familiar with is setTimeout function.

code

Closures

A closure is an inner function that has access to the outer function’s variables. It can access complete chain of variables. Illustration:

Closures

Closure function can access own variables, function variables in which it’s created and global variables. See from code perspective now,

  1. var globalVar = 10;  
  2.   
  3. function localFunction()  
  4. {  
  5.     var localFuncVar = 20;  
  6.     var closureFnc = function()  
  7.     {  
  8.         var closureVar = 30;  
  9.         console.log(globalVar);  
  10.         console.log(localFuncVar);  
  11.         console.log(closureVar);  
  12.     };  
  13.     closureFnc();  
  14. }  
output

I added debugger and checked Stack. We can notice Closure (localFunction) created.

code
Use of closure

A very popular use case is Module pattern, so it can implement OOPS public, private methods concept. Closure help to emulate this, example:
  1. var modularpattern = (function()  
  2. {  
  3.     // your module code goes here  
  4.     var sum = 0;  
  5.     return   
  6.     {  
  7.         add: function()   
  8.         {  
  9.             sum = sum + 1;  
  10.             return sum;  
  11.         },  
  12.         reset: function()   
  13.         {  
  14.             return sum = 0;  
  15.         }  
  16.     }  
  17. }());  
  18. console.log(modularpattern.add()); // 1  
  19. console.log(modularpattern.add()); // 2  
  20. console.log(modularpattern.reset()); // 0  
Stack: If you observe value of sum will increment or reset.

code

The objective is to hide variable accessibility from the outside world.

Disadvantage of closure

Memory leaks: Closure occupies memory and GC (Garbage collector) cannot collect memory within active closures. Hence, the chance of memory leaks is high if closure is not used well.

Debug: I found it comparatively difficult to debug closure if there is big nesting, i.e., function within function and so on....

Please share your feedback or comments.

Up Next
    Ebook Download
    View all
    Learn
    View all