Categories

Oct 19, 2019 in JavaScript
Q: Give an example of closure?

1 Answer

Oct 19, 2019
Following example shows how the variable counter is visible within the create, increment, and print functions, but not outside of them −

function create() {

   var counter = 0;

   return {

      increment: function() {

         counter++;

      },

  

      print: function() {

         console.log(counter);

      }

   }

}

var c = create();

c.increment();

c.print();     // ==> 1
Click here to read more about JavaScript
Click here to read more about Insurance

Related questions

Madanswer
Jan 27 in JAVA
May 30 in Node.Js
Jul 16, 2019 in JavaScript
...