whe*_*hys 5 javascript oop scope loops for-loop
在这个上绞尽脑汁.我有以下代码:JavaScript游戏的第一阶段.所有对象都是明确定义的,我正在使用jQuery进行DOM交互.该谜题是使用以下JS代码创建的:
var mypuzzle = new puzzle("{solution:'5+6+89',equations:[['5+3=8',23,23],['5+1=6',150,23],['5+3=6',230,23]]}");
Run Code Online (Sandbox Code Playgroud)
但是,代码底部的循环不会比第一次迭代更进一步.知道为什么吗?根本没有错误.
function equationBox(equation, top, left) {//draggable equation box
this.reposition = function() {
this.top = 0;
this.left = 0;
}
this.top = 0;//make random
this.left = 0;//make random
this.equation = equation;
if(top && left) {
this.top = top;
this.left = left;
}
this.content = this.equation.LHS.string + '<span> = </span>' + this.equation.RHS.string;
this.DOM = $('<li>').html(this.content);
}
function puzzle(json) {
this.addEquationBox = function(equationBox) {
$('#puzzle #equations').append(equationBox.DOM);
}
this.init = function() {
//this.drawPuzzleBox();
this.json = JSON.parse(json);
this.solution = new expression(this.json.solution || '');
this.equations = this.json.equations || [];
var iterations = this.equations.length;
for(i=0;i<iterations;i++)
{
console.log(i);
this.addEquationBox(new equationBox(stringToEquation(this.equations[i][0]),this.equations[i][1], this.equations[i][2]));
}
}
this.init();
}
Run Code Online (Sandbox Code Playgroud)
cha*_*aos 11
可能你未能对你的计数器变量进行调整,特别是如果你养成它的习惯(因为你正在使用该名称的全局变量,并且你在任何代码中编写的任何循环可能都是这样做的)事情).尝试:
for(var i=0;i<iterations;i++)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1577 次 |
| 最近记录: |