Javascript Data Type How to - Define an array of function, when the array is big








Question

We would like to know how to define an array of function, when the array is big.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
<!--from www .ja  va  2  s . c  o m-->
var f = []
for (var i = 0; i < 1000; i++) {
    (function (i) {
        f[i] = function () {
            return i;
        }
    })(i);
}
document.writeln(f[3]());
document.writeln('<br/>');
document.writeln(f[300]());
</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: