奇怪的 Javascript For 循环行为
我有一个像这样的 for 循环:
for (var i=first; i<=last; i++)
{
$("#markers").append("<div class='marker'>"+i+"</div>");
}
first
设置为 2001,last
为 2010。这工作正常。问题是当我将其更改为:(
for (var i=first; i<=last; i+=1)
{
$("#markers").append("<div class='marker'>"+i+"</div>");
}
注意不同的最终声明是不同的)。除 i++
之外的任何变化都会导致无限循环。这很奇怪,因为具有相同参数的 jsFiddle 可以愉快地工作。有什么建议吗?
I've got a for loop like this:
for (var i=first; i<=last; i++)
{
$("#markers").append("<div class='marker'>"+i+"</div>");
}
first
is set to 2001 and last
is 2010. This works fine. The problem is when I change it to:
for (var i=first; i<=last; i+=1)
{
$("#markers").append("<div class='marker'>"+i+"</div>");
}
(Notice the different final declaration is different). Any variation other than i++
results in an infinite loop. It's very strange as a jsFiddle with the same parameters works happily. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜想
first
的设置方式可能会被模糊地解释为string
。所以第一个版本只能解释为增量,但第二个版本被 javascript 解释为字符串连接。I would guess that
first
is set in a way that ambiguously could be interpreted as astring
. So the first version can only be interpreted as increment, but the second is being interpreted by javascript as string concatenation.