2015-10-02 71 views
0

使用數字屬性時出現什麼問題?我試圖做一個涉及數字的簡單計算,它返回NaN。使用數字屬性時操作不起作用(返回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

回答

4

錯誤拼寫的變量名。使用that.totalSeeds而不是that.totalseeds

+1

尼斯抓,謝謝。 https://www.youtube.com/watch?v=yI6VXlNRrI0 – jSmith

+0

你的JavaScript控制檯應該拋出一個錯誤,下次檢查它! :-) –

+0

我已經在我的控制檯中,查看我放入的console.logs,但沒有拋出任何錯誤 – jSmith

0

你有一個錯字:

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

更改爲

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