首页 > 解决方案 > 类中数字属性的乘法返回 Nan

问题描述

我在 chrome 中尝试了下面的 JavaScript 类示例代码,即使两个属性的数据类型都是数字,我也得到了 Nan 的输出。

class Sample
{
constructor(height,width)
{
    this.height = height;
    this.width = width;
}

calcArea()
{
    console.log ("Height : " + this.height + " Type : " + typeof this.height);
    console.log ("Width : " + this.width + " Type : " + typeof this.width);
    console.log( this.height * this.Width);
}
} 
var objSample = new Sample(10,20);
objSample.calcArea();

请在我在控制台中得到的输出下方找到。谢谢

Height : 10 Type : number
Width : 20 Type : number
NaN

标签: javascriptes6-class

解决方案


您的计算中有大小写错误。你this.height * this.Width应该做的时候this.height * this.width


推荐阅读