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
Nov 29, 2019 in Machine Learning
May 9 in DevOps
May 25 in JAVA
...