Answers for "closure declaration"

1

closure

var counter = (function() {  //exposed function references private state (the outer function’s scope / lexical environment) after outer returns. 
 var privateCounter = 0;
 function changeBy(val)   { privateCounter += val; }
 return {  increment: function() {changeBy(1); },
           decrement: function() {changeBy(-1);},
           value: function() {return privateCounter; }
  };
})();

counter.increment(); counter.increment();
counter.decrement();
counter.value();      //  1
Posted by: Guest on November-19-2020
0

closure

function add(a) {
     return function(b) {
         return a + b;
     };
 }

 let addTen = add(10);
 let addSeven = addTen(7);

 console.log(addSeven); // 17
Posted by: Guest on September-28-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language