I want to create some functions dynamically with javascript. all function do special work, but with different data(for example, console.log("data")
).
config = ['data1', 'data2', 'data3'];
returned = {};
for(var i in config){
returned[config[i]] = function(){console.log(config[i])};
}
I above code, All functions must log a data, but different data. I want to when I call returned["data1"]()
it log data1
, But when I call returned["data1"]()
it log data3
.
Is it possible? How can I do?