Javascript 类中数字属性的乘法返回Nan
我在chrome中尝试了下面的JavaScript类示例代码,虽然这两个属性的数据类型都是Number,但我得到的输出是NanJavascript 类中数字属性的乘法返回Nan,javascript,es6-class,Javascript,Es6 Class,我在chrome中尝试了下面的JavaScript类示例代码,虽然这两个属性的数据类型都是Number,但我得到的输出是Nan class Sample { constructor(height,width) { this.height = height; this.width = width; } calcArea() { console.log ("Height : " + this.height + " Type : " + typeof this.height)
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
你的计算中有一个套管误差。当它应该是
this.height*this.Width
时,您会执行this.height*this.Width
您的计算中存在大小写错误。当它应该是this.height*this.Width
时,您可以执行this.height*this.Width