更改后使其他两个变量的变量和

时间:2019-01-06 20:51:34

标签: javascript variables reference

请查看以下代码。我正在努力了解其中的一个问题:

let sideCount = 0;
let mealCount = 0;
let itemCount = sideCount + mealCount;

const pizzaOrder = ({size, crustType, topping, quantity}) => {
    quantity = Math.round(parseInt(quantity));
    mealCount += quantity;
    if (topping === "") {
        console.log(`Order: ${quantity} ${size.toTitleCase()} ${crustType.toLowerCase()} crust plain cheese pizza`);
    } else {
        console.log(`Order: ${quantity} ${size.toTitleCase()} ${crustType.toLowerCase()} crust ${topping} pizza`);
    }
    console.log(`Meal Count after ordering ${quantity} pizzas: ${mealCount}`);
    console.log(`Current item count: ${itemCount}`);        
};

(还有更多,但这对这个问题很重要)

我在整个StackOverflow上都查找了引用,并按值传递或按引用传递,但是找不到任何解决我问题的方法。

当我更新mealCount时,它不会更改itemCount的值。我该如何解决?

Order: 10 Medium thin crust pepperoni pizza 
Meal Count after ordering 10 pizzas: 10 
Current item count: 0 
Order: 3 burgers with Relish 
Meal Count after ordering burger: 13 
Current item count: 0 
Meal Count: 0 Side Count: 0 
Meal Count: 0 
Side Count: 0 
Meal Count: 0 
Side Count: 0 
Your total is $0.00. Enjoy your meal! 

3 个答案:

答案 0 :(得分:1)

代码按顺序解释(评估)。

当您写: 让itemCount = sideCount + mealCount;

您实际上将sideCount + mealCount的值分配给itemCount。此时,sideCount和饭食计数的值为“ 0”,因此itemCount等于“ 0”。

您调用函数pizzaOrder和更改mealCount(在此函数内部)将不会使javascript重新评估所提到的行(让itemCount = sideCount + mealCount)。

从字面上看,pizzaOrder函数根本不影响变量“ itemCount”。

一种解决方案是添加以下行: pizzaOrder函数中的“ itemCount = sideCount + mealCount”,特别是在以下行:“ mealCount + =数量;”。

因此,您的代码如下:

const pizzaOrder = ({size, crustType, topping, quantity}) => {
    quantity = Math.round(parseInt(quantity));
    mealCount += quantity;
    totalCount = mealCount + sideCount;
    if (topping === "") {
        console.log(`Order: ${quantity} ${size.toTitleCase()} ${crustType.toLowerCase()} crust plain cheese pizza`);
    } else {
        console.log(`Order: ${quantity} ${size.toTitleCase()} ${crustType.toLowerCase()} crust ${topping} pizza`);
    }
    console.log(`Meal Count after ordering ${quantity} pizzas: ${mealCount}`);
    console.log(`Current item count: ${itemCount}`);        
};

答案 1 :(得分:1)

如果要保留订单的“状态”,则可能需要使用可以在需要的地方公开的对象:

const count = {
sideCount: 0,
mealCount: 0,
itemCount() {
    return this.sideCount + this.mealCount
  }
}

const pizzaOrder = ({size, crustType, topping, quantity}) => {
quantity = Math.round(parseInt(quantity));
count.mealCount += quantity;
if (topping === "") {
    console.log(`Order: ${quantity} ${size.toTitleCase()} ${crustType.toLowerCase()} crust plain cheese pizza`);
} else {
    console.log(`Order: ${quantity} ${size} ${crustType.toLowerCase()} crust ${topping} pizza`);
}
console.log(`Meal Count after ordering ${quantity} pizzas: ${count.mealCount}`);
console.log(`Current item count: ${count.itemCount()}`);        
};
console.log(count.itemCount()) // 0

pizzaOrder({size: 'asd', crustType: 'asd', topping: 'asd', quantity: 1})

console.log(count.itemCount()) // 1

正如其他人提到的那样,您的代码将无法正常工作。 希望这会有所帮助。

答案 2 :(得分:0)

我没有在每个函数中添加mealCount,而是在对象中返回了mealCountsideCountdrinkCount,就像这样

return {mealCount: quantity};

然后,我在Array.prototype.reduce()函数的orders参数的groupOrder()方法上使用了let mealCount = orders.reduce((av, cv) => { if (cv.hasOwnProperty('mealCount')) { return {mealCount: av.mealCount + cv.mealCount}; } return {mealCount: av.mealCount + 0}; }, {mealCount: 0}).mealCount; 方法,您将这个函数调用,将一系列函数调用传递给食品订单,嵌套在其中的食品订单和价格计算

re.sub