const markProperties = {
fullName: 'Mark Miller',
mass: 78,
height: 1.69,
calcBMI: function () {
return this.mass / this.height ** 2;
},
bmi: calcBMI()
}
const johnProperties = {
fullName: 'John Smith',
mass: 92,
height: 1.95,
calcBMI: function () {
this.bmi = this.mass / this.height ** 2;
return this.bmi;
},
bmi: calcBMI()
};
const checkWinner = () => {
if (johnProperties.bmi > markProperties.bmi) {
return "John's BMI (" johnProperties.bmi ") is higher than Mark's BMI (" markProperties.bmi ")";
} else if (markProperties.bmi > johnProperties.bmi) {
return "Mark's BMI (" markProperties.bmi ") is higher than John's BMI (" johnProperties.bmi ")";
}
}
console.log(checkWinner());
這是代碼,它說兩個物件中的函式都沒有定義。正如我所說,它帶來了一個錯誤,內容為:錯誤:未捕獲的參考錯誤:未定義 calcBMI
uj5u.com熱心網友回復:
定義物件時,不能執行物件中定義的函式。在您的情況下,您應該簡單地為bmi屬性設定一個吸氣劑:
const markProperties = {
fullName: 'Mark Miller',
mass: 78,
height: 1.69,
get bmi() {
return this.mass / this.height ** 2;
}
}
const johnProperties = {
fullName: 'John Smith',
mass: 92,
height: 1.95,
get bmi() {
return this.mass / this.height ** 2;
}
};
const checkWinner = () => {
if (johnProperties.bmi > markProperties.bmi) {
return "John's BMI (" johnProperties.bmi ") is higher than Mark's BMI (" markProperties.bmi ")";
} else if (markProperties.bmi > johnProperties.bmi) {
return "Mark's BMI (" markProperties.bmi ") is higher than John's BMI (" johnProperties.bmi ")";
}
}
console.log(checkWinner());
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/436042.html
標籤:javascript 功能 错误处理
