未捕获的引用错误:(函数)未定义 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());
这是代码,它表示两个对象内的函数未定义。正如我所说,它带来了一个错误,内容为: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
定义对象时,您无法执行该对象中定义的函数。
在您的情况下,您应该简单地为
bmi
属性设置一个 getter: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: