by mkoryak · 11y ago · 0 comments
- bind
- function creator outside of the loop called from within
- create an anon function on each loop iteration that acts like the function creator in #2
The bind is best in terms of memory and simplicity, but will not run on on IE 8 or crappier, so in that case #2 is preferred.The last one creates anon functions needlessly, but is very compact to write.
function foo(s) {
a = Array(s);
for (var i = 0; i < s; i++) {
a[i] = eval('(function () { return ' + i + '})');
}
return a;
}