in JavaScript by
Q:
Give an example of closure?

1 Answer

0 votes
by
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

0 votes
asked Jun 15, 2020 in C Plus Plus by Robindeniel
0 votes
asked Oct 18, 2019 in C Sharp by Robin
+1 vote
asked May 30, 2020 in Node.Js by SakshiSharma
+1 vote
asked Jul 16, 2019 in JavaScript by Indian
0 votes
asked Mar 7, 2020 in Spark Sql by rahuljain1
...