Return function from a function and save it to an array - Javascript Array Operation

Javascript examples for Array Operation:Array Element

Description

Return function from a function and save it to an array

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
   </head> 
   <body> 
      <script type="text/javascript">
var a = [];
for (i = 0; i < 10; i++) {
    a[i] = alertFunc(i);
}
function alertFunc(i){
    return function() {// ww  w.jav a 2 s  .c om
        console.log(i);
    };
};
a[5]();

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

Related Tutorials