【发布时间】:2015-04-19 06:22:40
【问题描述】:
我有一些形式的对象构造函数的代码:
function objectConstructor(item, r) {
//Pulls a string from elsewhere
this.name = Object.ObjectOne[item].name;
this.amount = Object.ObjectOne[item].amount;
this.buttonID = pickButton(r);
this.itemInitialCost = [];
this.itemPriceIncrease = [];
//This is the problematic area.
if(this.name != "valueOne" || this.name != "valueTwo" || this.name != "valueThree" || this.name != "valueFour" || [...]) {
for(var i = 0; i < Object.ObjectTwo[this.buttonID].cost.length; i++) {
this.itemInitialCost.push(Object.ObjectTwo[this.buttonID].cost[i].initialCost;
this.itemPriceIncrease.push(Object.ObjectTwo[this.buttonID].cost[i].costIncrease;
}
} else {
this.itemInitialCost.push("none");
this.itemPriceIncrase.push("none");
}
}
function pickButton(r) {
//The Function
}
var array = [Long List];
for(var i = 0; i < array.length; i++) {
item = array[i];
items[item] = new objectConstructor(item, i);
console.log("Created: " + item);
}
console.log(items);
创建前 58 个(共 67 个)项目对象并为其赋值。 58 号之后的所有项目都没有 buttonID,因此在尝试获取成本值时会抛出错误。为了防止这种情况,我设置了 if 语句来为这些项目(基于项目名称)分配成本的“无”值。但是,他们似乎忽略了 if 语句中的条件并直接进入设置正常项目成本的 for 循环。他们不会去 else 语句,从而导致我的脚本崩溃。
为什么他们显然忽略了 if 语句的条件?
【问题讨论】:
标签: javascript if-statement conditional-statements