簡體   English   中英

未捕獲的引用錯誤:(函數)未定義 JAVASCRIPT

[英]Uncaught Reference Error: (function) is not defined JAVASCRIPT


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());

這是代碼,它表示未定義兩個對象中的 function。 正如我所說,它帶來了一個錯誤,內容如下:錯誤:未捕獲的引用錯誤:未定義 calcBMI

當您定義 object 時,您不能執行在 object 中定義的 function。在您的情況下,您應該簡單地為bmi屬性設置一個 getter:

 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());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM