问题描述
在阅读了有关hoisting issue的信息后,我使用VAR进行了初始化,我发现它们在for循环中正确更新,但是最后它们具有它们的起始值,所以我还必须犯其他错误。
这是我的代码的简化版:
items = [];
totalPrice = 0;
for (let [key,val] of lineItems.entries()){
console.log("toyID " + key + ",with quantity: " + val);
Toy.findOne( { id: key },(err,toy) => {
if (parseInt(val) > 0){
var subtotal = val * toy.price;
var thisItem = {"item": key,"qty": val,"subtotal": subtotal};
totalPrice += subtotal; // shows correctly updated price
items.push(thisItem);
console.log("items Now has: ",items); // shows items
}
});
}
console.log("At the end,totalPrice has: ",totalPrice); // here this is 0
console.log("At the end,Items has: ",items); // here this is empty again
这些全局变量保持其更新值的方式是否还会出现皱纹?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)