有时当我调用此对象的方法时,我收到“糟糕”错误,因为“i”指的是不存在的属性。怎么可能?
m.derp();
m.herp(); // Sometimes throws error
方法:
this.movements = new Array();
this.herp = function() {
for (var i in this.movements) {
if (!(this.movements[i] instanceof Movement)) {
throw new Error("Oops"); // this.movements[i] is undefined
}
}
}
this.derp = function() {
var newArray = new Array();
for (var i in this.movements) {
if (!this.movements[i].isFinished()) {
newArray.push(this.movements[i]);
}
}
this.movements = newArray;
}