当我使用buyDrink()方法运行代码时,它被认为是可行的。
但是,如果不对方法进行注释,则不是。我试图在几个小时内解决问题,但没有成功。
请问你能帮帮我吗?
谢谢。
var coffeeShop = {
beans: 40,
money: 100,
beanCost: 2,
drinkRequirements: {
latte: {
beansRequirements: 10,
price: 5
},
americano: {
beansRequirements: 5,
price: 2.5
},
doubleShot: {
beansRequirements: 15,
price: 7.5
},
frenchPress: {
beansRequirements: 12,
price: 6
}
},
buySupplies: function(numBeansBought) {
if (this.money - (numBeansBought*this.beanCost) >= 0)
{
this.money-= numBeansBought*this.beanCost;
this.beans+=numBeansBought;
}
},
makeDrink: function(drinkType) {
/*
How to check if value exists in an object
Let's consider an example which would answer this question
var priceOfFood = { pizza: 14, burger 10 }
priceOfFood.hasOwnProperty('pizza') // true
priceOfFood['pizza'] // 14
priceOfFood.hasOwnProperty('chips') // false
priceOfFood['chips'] // undefined
*/
//if (this.drinkRequirements.hasOwnProperty(drinkType) === false)
if (!this.drinkRequirements[drinkType])
{
alert("Sorry, we don't make "+ drinkType);
return false;
}
else
{
var beansRest = 0;
beansRest = this.beans - this.drinkRequirements[drinkType].beansRequirements;
if (beansRest < 0)
{
alert("Sorry, we're all out of beans!");
return false;
}
else
{
this.beans = beansRest;
return true;
}
}
}
,
buyDrink: function(drinkType) {
if (makeDrink(drinkType))
{
this.money+= drinkRequirements[drinkType].price;
console.log('the new coffeeShop money is ' + this.money);
}
}
}
coffeeShop.makeDrink("latte");
console.log("latte, " + coffeeShop.beans);
coffeeShop.buyDrink("latte");
console.log("latte, " + coffeeShop.beans);
我有此错误信息
:
“未捕获的ReferenceError:未定义makeDrink”,
你可以帮我吗?
谢谢,
问候
最佳答案
您需要引用调用上下文-this
,它引用正在调用当前函数的对象的coffeeShop
,该对象具有makeDrink
和drinkRequirements
属性。它们不是独立的函数/对象-它们是另一个对象的属性。
var coffeeShop = {
beans: 40,
money: 100,
beanCost: 2,
drinkRequirements: {
latte: {
beansRequirements: 10,
price: 5
},
americano: {
beansRequirements: 5,
price: 2.5
},
doubleShot: {
beansRequirements: 15,
price: 7.5
},
frenchPress: {
beansRequirements: 12,
price: 6
}
},
buySupplies: function(numBeansBought) {
if (this.money - (numBeansBought * this.beanCost) >= 0) {
this.money -= numBeansBought * this.beanCost;
this.beans += numBeansBought;
}
},
makeDrink: function(drinkType) {
/*
How to check if value exists in an object
Let's consider an example which would answer this question
var priceOfFood = { pizza: 14, burger 10 }
priceOfFood.hasOwnProperty('pizza') // true
priceOfFood['pizza'] // 14
priceOfFood.hasOwnProperty('chips') // false
priceOfFood['chips'] // undefined
*/
//if (this.drinkRequirements.hasOwnProperty(drinkType) === false)
if (!this.drinkRequirements[drinkType]) {
alert("Sorry, we don't make " + drinkType);
return false;
} else {
var beansRest = 0;
beansRest = this.beans - this.drinkRequirements[drinkType].beansRequirements;
if (beansRest < 0) {
alert("Sorry, we're all out of beans!");
return false;
} else {
this.beans = beansRest;
return true;
}
}
}
,
buyDrink: function(drinkType) {
if (this.makeDrink(drinkType)) {
this.money += this.drinkRequirements[drinkType].price;
console.log('the new coffeeShop money is ' + this.money);
}
}
}
coffeeShop.makeDrink("latte");
console.log("latte, " + coffeeShop.beans);
coffeeShop.buyDrink("latte");
console.log("latte, " + coffeeShop.beans);
关于javascript - 未捕获的ReferenceError:未定义makeDrink,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49937248/