javascript - 类中数字属性的乘法返回 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
解决方案
您的计算中有大小写错误。你this.height * this.Width
应该做的时候this.height * this.width
推荐阅读
- c# - 如何从基础对象数组中的子对象检索数据?
- java - 如何从 javax.lang.model.VariableElement 获取类型和泛型类型?
- java - 共享首选项中的 getBoolean 方法有问题,尽管磁盘上的值已更改,但始终显示为真
- python - 如何匹配选定的二元组列表和标记推文字典列表
- python - 如何按列上的条件更改行值(python,pandas)
- mysql - phpMyAdmin 在不断更新的表上更改存储引擎
- docker - 如何使用 docker swarmn 从我的前端 docker nginx 调用 nodeJs docker 中的后端
- node.js - 有没有办法让我的代码不区分大小写
- c - C程序循环重复两次而不是一次
- go - 将全局和外部上下文作为参数传递给 Hugo 部分模板