Javascript中循环的奇怪行为
下面的代码是生成公告栏列表的代码。它可以很好地查看内容(例如,postId0、postId1等),但其工作原理就像“i”是npost一样 我记得在C#中,我遇到了同样的问题,并通过声明I的一个副本(var copy#u I=I;在循环中)进行了修复,但在这里,这也不起作用Javascript中循环的奇怪行为,javascript,for-loop,closures,Javascript,For Loop,Closures,下面的代码是生成公告栏列表的代码。它可以很好地查看内容(例如,postId0、postId1等),但其工作原理就像“i”是npost一样 我记得在C#中,我遇到了同样的问题,并通过声明I的一个副本(var copy#u I=I;在循环中)进行了修复,但在这里,这也不起作用 function loadList() { $.getJSON("list.php", function (json) { var nPosts = json.length; for (
function loadList() {
$.getJSON("list.php", function (json) {
var nPosts = json.length;
for (var i = 0; i < nPosts; i++) {
var post = $('<ol/>', {
class: "viewPost",
id: "post" + i
}).appendTo("#viewList");
$('<li/>', {
class: "viewPostId",
id: "postId" + i,
text: json[i].noteId
}).appendTo("#post" + i);
var memo = $('<li/>', {
class: "viewMemo",
id: "memo" + i,
text: json[i].noteContent
}).appendTo("#post" + i);
//alerts 'nPosts' for every i.
memo.on("click", function () { alert(i); });
}
});
}
函数加载列表(){
$.getJSON(“list.php”,函数(json){
var nPosts=json.length;
对于(变量i=0;i ”{
类:“viewPostId”,
id:“posted”+i,
text:json[i].noteId
}).附于(“#post”+i);
var memo=$(“”{
类:“查看备忘录”,
id:“备忘录”+i,
text:json[i].noteContent
}).附于(“#post”+i);
//为每个i发出“NPOST”警报。
memo.on(“单击”,函数(){alert(i);});
}
});
}
在循环完成后执行警报i的单击功能,因此它将等于NPOST在循环完成后执行警报i的单击功能,因此它将等于NPOST在循环完成后执行警报i的单击功能,因此它将等于nPosts在循环完成后执行警报i的单击功能,因此它将等于nPosts正如预期的那样,问题在于意外的关闭。这一行:
memo.on("click", function () { alert(i); });
在i
变量上创建一个闭包,该变量在调用click
处理程序之前一直递增到最大值
下面是如何为处理程序提供一个“虚拟变量”来结束:
var handlerCreator = function (counter) {
return function(){
alert(counter); // The handler now closes over the counter variable,
// which cannot be accessed by other code and therefore
// retains the value passed in to this function (i.e. the
// value of i on the current iteration)
};
}
memo.on("click", handlerCreator(i));
正如所料,问题在于意外的关闭。这一行:
memo.on("click", function () { alert(i); });
在i
变量上创建一个闭包,该变量在调用click
处理程序之前一直递增到最大值
下面是如何为处理程序提供一个“虚拟变量”来结束:
var handlerCreator = function (counter) {
return function(){
alert(counter); // The handler now closes over the counter variable,
// which cannot be accessed by other code and therefore
// retains the value passed in to this function (i.e. the
// value of i on the current iteration)
};
}
memo.on("click", handlerCreator(i));
正如所料,问题在于意外的关闭。这一行:
memo.on("click", function () { alert(i); });
在i
变量上创建一个闭包,该变量在调用click
处理程序之前一直递增到最大值
下面是如何为处理程序提供一个“虚拟变量”来结束:
var handlerCreator = function (counter) {
return function(){
alert(counter); // The handler now closes over the counter variable,
// which cannot be accessed by other code and therefore
// retains the value passed in to this function (i.e. the
// value of i on the current iteration)
};
}
memo.on("click", handlerCreator(i));
正如所料,问题在于意外的关闭。这一行:
memo.on("click", function () { alert(i); });
在i
变量上创建一个闭包,该变量在调用click
处理程序之前一直递增到最大值
下面是如何为处理程序提供一个“虚拟变量”来结束:
var handlerCreator = function (counter) {
return function(){
alert(counter); // The handler now closes over the counter variable,
// which cannot be accessed by other code and therefore
// retains the value passed in to this function (i.e. the
// value of i on the current iteration)
};
}
memo.on("click", handlerCreator(i));
我现在正在通读代码,但作为一个初步问题:您是否熟悉闭包的概念(以及计数器变量如何也可以闭包)?通常关于循环中奇怪行为的问题是由于忽略闭包而引起的。我现在正在通读代码,但作为一个初步问题:您熟悉闭包的概念吗(以及计数器变量如何也可以被闭包)?通常关于循环中奇怪行为的问题是由于忽略闭包而引起的。我现在正在通读代码,但作为一个初步问题:您熟悉闭包的概念吗(以及计数器变量如何也可以被闭包)?通常关于循环中奇怪行为的问题是由于忽略闭包而引起的。我现在正在通读代码,但作为一个初步问题:您熟悉闭包的概念吗(以及计数器变量如何也可以被闭包)?通常关于循环中奇怪行为的问题是由于忽略闭包而引起的。谢谢!因此,伪变量方法在js中不起作用,应该始终定义函数创建者。我想可能会有一些更简单的解决办法。谢谢!因此,伪变量方法在js中不起作用,应该始终定义函数创建者。我想可能会有一些更简单的解决办法。谢谢!因此,伪变量方法在js中不起作用,应该始终定义函数创建者。我想可能会有一些更简单的解决办法。谢谢!因此,伪变量方法在js中不起作用,应该始终定义函数创建者。我想可能会有一些更简单的解决办法。