kaniosrn-j
6/11/2017 - 7:45 AM

example of closures

example of closures

let foo = [];

for(var i =0; i < 10; i++){
  (function(){
   var y = i;
   foo[i] = function() { 
     return y 
   };
  })();
}

console.log(foo[0]()); // 0
console.log(foo[1]()); // 1
console.log(foo[2]()); // 2