未捕获的引用错误:(函数)未定义 JAVASCRIPT

发布于 2025-01-11 00:08:50 字数 953 浏览 0 评论 0原文


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

这是代码,它表示两个对象内的函数未定义。正如我所说,它带来了一个错误,内容为: error: Uncaught ReferenceError: calcBMI is not Defined


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

This is the code and it says that the function inside both objects is not defined. As I said, It brings an error that reads: error: Uncaught ReferenceError: calcBMI is not defined

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞陪衬 2025-01-18 00:08:50

定义对象时,您无法执行该对象中定义的函数。
在您的情况下,您应该简单地为 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());

When you defining an object, you can't execute a function that is being defined in the object.
In your case you should simply set a getter for bmi property instead:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文