Gad*_*i A 1 javascript for-loop
在Javascript程序中,我有一个具有以下两个(简化)函数的对象:
this.do_A = function() {
var nothing_changed = false;
while (!nothing_changed) {
nothing_changed = true;
for (var column=0; column<this.columns; column++) {
for (var row=0; row<this.rows; row++) {
nothing_changed = nothing_changed && this.do_B(row, column);
}
}
}
}
this.do_B = function(row, column) {
nothing_changed = true;
if (this[row][column] == null) {
nothing_changed = false;
}
return nothing_changed;
}
Run Code Online (Sandbox Code Playgroud)
当运行这段代码时,当do_B返回false时会发生一些非常奇怪的事情,因此当又到达时,nothing_changed变为false
for (var row=0; row<this.rows; row++)
Run Code Online (Sandbox Code Playgroud)
行,row变量立即变为this.rows内部循环终止.此外,它发生在外部循环的后续运行中 - row初始化为0,然后this.rows立即变为内部循环再次结束.
我没有理由可以导致这种情况.我已经尝试尽可能地简化功能并且它一直在发生.
for (var row=0; row<this.rows; row++)
{
nothing_changed = nothing_changed && this.do_B(row, column);
}
Run Code Online (Sandbox Code Playgroud)
当this.do_B(row, column)回报false,nothing_changed将是false
,当它再次循环,达到nothing_changed = nothing_changed && this.do_B(row, column),因为nothing_changed是false,第二个表达式this.do_B(row, column)将不进行评估,所以nothing_changed永远是false直到row到达this.rows.
| 归档时间: |
|
| 查看次数: |
259 次 |
| 最近记录: |