admin管理员组文章数量:1330609
I am using a function with the following parameterization (which cannot be changed):
my_function(data, callback_function(results, status) {});
I need to pass additional information to callback_function that cannot be added to 'data' (which callback_function uses) or 'results' or 'status'. Specifically this information is the counter of a for loop that the my_function call is in.
To do this I am including a reference to the counter inside the body of callback_function:
for(var i = 0; i < 10; i++) {
var data = 'cannot modify this data';
my_function(data, function (results, status) { alert(i); });
}
Unfortunately the final value of i (9 in this case) is being printed 10 times. The intended behavior is for each value of i in the loop (0 through 9) to be printed.
Is it possible for dynamic functions to access variables outside of their scope but within the scope that they are defined?
I am using a function with the following parameterization (which cannot be changed):
my_function(data, callback_function(results, status) {});
I need to pass additional information to callback_function that cannot be added to 'data' (which callback_function uses) or 'results' or 'status'. Specifically this information is the counter of a for loop that the my_function call is in.
To do this I am including a reference to the counter inside the body of callback_function:
for(var i = 0; i < 10; i++) {
var data = 'cannot modify this data';
my_function(data, function (results, status) { alert(i); });
}
Unfortunately the final value of i (9 in this case) is being printed 10 times. The intended behavior is for each value of i in the loop (0 through 9) to be printed.
Is it possible for dynamic functions to access variables outside of their scope but within the scope that they are defined?
Share edited Feb 7, 2012 at 19:22 kanoko asked Feb 7, 2012 at 0:20 kanokokanoko 851 gold badge2 silver badges8 bronze badges 4- your already using closures,i think.. and the value 9 is a reference to i -- which is changed to 9 at the end of the loop and hence prints 9 -- always!! – Vivek Chandra Commented Feb 7, 2012 at 19:38
-
if your trying to define the myfunction with the values provided by
i
and later trying to call it -- its a closure..!!.. its accessing the reference to i.. – Vivek Chandra Commented Feb 7, 2012 at 19:39 - @VivekChandra The second part of my code is the actual call to my_function, not the definition. And I don't believe my_function is a closure since i is not being passed as a parameter. – kanoko Commented Feb 7, 2012 at 22:00
- @Vivek: integers are no reference types in JS. Plus, he is not creating a closure. – Niklas B. Commented Feb 7, 2012 at 22:28
3 Answers
Reset to default 6You need to create a closure that contains the value of i
at the time where the anonymous function is created. You can use a wrapper function for that:
function createClosure(x, func) {
return function(results, status) { func(x, results, status); }
}
/* ... */
for(var i = 0; i < 10; i++) {
var data = 'cannot modify this data';
my_function(data, createClosure(i, function(i, results, status) {
alert(i);
alert(results);
alert(status);
}));
}
Or if you want to be short, you can create the closure in place:
for(var i = 0; i < 10; i++) {
var data = 'cannot modify this data';
my_function(data, (function(i) {
return function (results, status) {
alert(i);
}
})(i));
}
What you need is Function.prototype.bind. Using that you can bind a function to certain parameters. With that in place, your code will look like this:
for(var i = 0; i < 10; i++) {
var data = 'cannot modify this data';
function callback(iValue, results, status) { alert(iValue); }
my_function(data, callback.bind(null, i)); // |null| since you don't seem to need any specific |this|.
}
for(var i = 0; i < 10; i++) {
var data = 'cannot modify this data';
my_function(data, function (results, status) {
alert(
function(value){
return value;
}(i);
)
} //function (results, status) ends here
); // myfunction ends here..
}
Not tested it,but .. try it.. hope it works..
Check this link -> Javascript closures - variable scope question for a better understanding of what you are doing..
本文标签:
版权声明:本文标题:Javascript: Passing local variables into a dynamically created function that is a parameter inside another function - Stack Over 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742266761a2443547.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论