Class 方法不会给出正确的输出
Class method won't give the right output
class CoffeeShop {
constructor(name, menu) {
this.name = name;
this.menu = menu;
this.orders = [];
}
addOrder(itemName) {
this.menu.reduce((acc, meal, i, arr) => {
meal[itemName] ? (this.orders.push(itemName), console.log("Order Added!"), arr.splice(1)) : (console.log("This item is currently unavailable!"), arr.splice(1))
}, [])
}
fulfillOrder() {
this.orders.isEmpty ? (console.log("All orders have been fulfilled!")) : (console.log(`The ${this.orders[0]} is ready!`), this.orders.shift())
}
}
let Shop = new CoffeeShop("Shop", [
{ item: "cinnamon roll", type: "food", price: 4.99},
{ item: "hot chocolate", type: "drink", price: 2.99},
{ item: "lemon tea", type: "drink", price: 2.50}
]);
Shop.addOrder("hot chocolate");
console.log(Shop.orders);
Shop.fulfillOrder();
console.log(Shop.orders);
输出为:
This item is currently unavailable! [] The undefined is ready! []
为什么订单列表没有变化?为什么 fullfillOrder 函数 return 与它应该 return 相反,即使条件为真?
我宁愿使用过滤器 - 过滤掉添加的项目
class CoffeeShop {
constructor(name, menu) {
this.name = name;
this.menu = menu;
this.orders = [];
}
addOrder(itemName) {
this.menu = this.menu.filter(i => {
if(i.item === itemName) {
this.orders.push(itemName),
console.log("Order Added!")
return false;
}
console.log("This item is currently unavailable!")
return true;
})
}
fulfillOrder() {
this.orders.isEmpty ? (console.log("All orders have been fulfilled!")) : (console.log(`The ${this.orders[0]} is ready!`), this.orders.shift())
}
}
let Shop = new CoffeeShop("Shop", [
{ item: "cinnamon roll", type: "food", price: 4.99},
{ item: "hot chocolate", type: "drink", price: 2.99},
{ item: "lemon tea", type: "drink", price: 2.50}
]);
Shop.addOrder("hot chocolate");
console.log(Shop.orders);
Shop.fulfillOrder();
console.log(Shop.orders);
class CoffeeShop {
constructor(name, menu) {
this.name = name;
this.menu = menu;
this.orders = [];
}
addOrder(itemName) {
this.menu.reduce((acc, meal, i, arr) => {
meal[itemName] ? (this.orders.push(itemName), console.log("Order Added!"), arr.splice(1)) : (console.log("This item is currently unavailable!"), arr.splice(1))
}, [])
}
fulfillOrder() {
this.orders.isEmpty ? (console.log("All orders have been fulfilled!")) : (console.log(`The ${this.orders[0]} is ready!`), this.orders.shift())
}
}
let Shop = new CoffeeShop("Shop", [
{ item: "cinnamon roll", type: "food", price: 4.99},
{ item: "hot chocolate", type: "drink", price: 2.99},
{ item: "lemon tea", type: "drink", price: 2.50}
]);
Shop.addOrder("hot chocolate");
console.log(Shop.orders);
Shop.fulfillOrder();
console.log(Shop.orders);
输出为:
This item is currently unavailable! [] The undefined is ready! []
为什么订单列表没有变化?为什么 fullfillOrder 函数 return 与它应该 return 相反,即使条件为真?
我宁愿使用过滤器 - 过滤掉添加的项目
class CoffeeShop {
constructor(name, menu) {
this.name = name;
this.menu = menu;
this.orders = [];
}
addOrder(itemName) {
this.menu = this.menu.filter(i => {
if(i.item === itemName) {
this.orders.push(itemName),
console.log("Order Added!")
return false;
}
console.log("This item is currently unavailable!")
return true;
})
}
fulfillOrder() {
this.orders.isEmpty ? (console.log("All orders have been fulfilled!")) : (console.log(`The ${this.orders[0]} is ready!`), this.orders.shift())
}
}
let Shop = new CoffeeShop("Shop", [
{ item: "cinnamon roll", type: "food", price: 4.99},
{ item: "hot chocolate", type: "drink", price: 2.99},
{ item: "lemon tea", type: "drink", price: 2.50}
]);
Shop.addOrder("hot chocolate");
console.log(Shop.orders);
Shop.fulfillOrder();
console.log(Shop.orders);