使用数字属性的问题是什么?我试图进行一个涉及数字的简单计算,然后返回NaN。
function test () {
var that = this;
this.usersCount = 2;
this.totalSeeds = 10;
this.test2 = function () {
console.log(2/10*100); // 20
console.log(that.usersCount * that.totalSeeds); // 20
var percentUsersCount = that.usersCount / that.totalseeds * 100; // also tried parseInt() and Number()
console.log(percentUsersCount); // NaN -- WHY !?!
}
}
var test = new test();
test.test2();
var test1 = 2;
var test2 = 10;
var percent = test1 / test2 * 100;
console.log(percent); // 20
为什么percentUsersCount
NaN?
答案 0 :(得分:4)
您错误拼写了变量的名称。使用that.totalSeeds
代替that.totalseeds
。
答案 1 :(得分:0)
你有一个错字:
var percentUsersCount = that.usersCount / that.totalseeds * 100;
更改为
var percentUsersCount = that.usersCount / that.totalSeeds * 100;