Storing closure functions within array using loop - Javascript Array Operation

Javascript examples for Array Operation:Array Element

Description

Storing closure functions within array using loop

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body> 
      <script>
var students = ["a", "b", "c", "d"];
var _class = students.map(function(name, i) {
    return function() {//from w ww.  j  av a  2s .co m
        console.log('Is', name, 'present?', i);
    };
});
console.log(_class[0]());
console.log(_class[1]());
console.log(_class[2]());
console.log(_class[3]());

      </script>  
   </body>
</html>

Related Tutorials