Joh*_* L. 1 javascript arrays for-loop
this.draw = function() {
console.log(this.buttonList.length);
for(a = 0; a < this.buttonList.length; a++) {
console.log(this.buttonList.length, a);
this.buttonList[a].draw();
}
};
Run Code Online (Sandbox Code Playgroud)
所以我在一个对象中有这个功能,并且它没有像我预期的那样工作.当我使用上述console.log语句运行它时,它会在控制台上记录它:
2
2 0
Run Code Online (Sandbox Code Playgroud)
这似乎告诉我,我的for循环并没有循环遍历阵列上的每个项目,而且我一直在思索为什么那将是相当长一段时间.有谁知道为什么它只执行a = 0的动作?
编辑:我不知道这是否有所不同,但这个函数每秒调用约60次.
添加var可能会解决它:
this.draw = function() {
console.log(this.buttonList.length);
for(var a = 0; a < this.buttonList.length; a++) {
console.log(this.buttonList.length, a);
this.buttonList[a].draw();
}
};
Run Code Online (Sandbox Code Playgroud)
您的代码可能还有另外一点需要改变a.你应该小心变量声明.
| 归档时间: |
|
| 查看次数: |
857 次 |
| 最近记录: |