使用数字属性时操作无效(返回NaN)

时间:2015-10-02 10:17:02

标签: javascript

使用数字属性的问题是什么?我试图进行一个涉及数字的简单计算,然后返回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?

http://jsfiddle.net/ht2rv1ea/

修改:https://www.youtube.com/watch?v=yI6VXlNRrI0

2 个答案:

答案 0 :(得分:4)

您错误拼写了变量的名称。使用that.totalSeeds代替that.totalseeds

答案 1 :(得分:0)

你有一个错字:

  var percentUsersCount = that.usersCount / that.totalseeds * 100;

更改为

  var percentUsersCount = that.usersCount / that.totalSeeds * 100;