Javascript Create A List Of Functions Dynamically
Solution 1:
You need to give each instance of the function its own variable:
for(var l = a.length, i = 0; i < l; i++)
{
(function (x) {
d[a[x]] = function(){console.log(a[x])};
})(i)
}
Solution 2:
They don't point to the same instance of function(){console.log(a[i])}
, instead, you've created a bunch of functions that all use the same reference to i
. The value that i
points at changes as the for
loop executes.
The other answers provided will work, but it involves generating twice as many functions as you need.
functionmakeLogFunction(whatToLog) {
returnfunction() {
console.log(whatToLog);
}
}
var a = "abcdefghijklmnopqrstuvwxyz1234567890";
var d = {};
for(var l = a.length, i = 0; i < l; i++) {
d[a[i]] = makeLogFunction(a[i]);
}
Here, I have a makeLogFunction
that will return a new function that always prints whatToLog
. The other answers will generate a new "version" of the makeLogFunction every time the loop executes. For very large sets of data, this is a waste of time and memory.
This approach has added advantages of clarity and reusability. If there's a significant amount of logic happening in your loop, encapsulating it in a named function allows future reviewers to get a sense of what's going on by the name you give to the function. You can also reuse the function in other parts of your application.
Post a Comment for "Javascript Create A List Of Functions Dynamically"