typeof number + typeof number = NaN?

时间:2012-02-05 10:28:30

标签: javascript

为什么这是JavaScript中的NaN

alert(typeof settings.mouse.x + ' --- ' + typeof getScalePercent((bg_w - a_w)/2, settings.bg.perc_position));
// alerts: number --- number

var pos_x = settings.mouse.x - getScalePercent((bg_w - a_w)/2, settings.bg.perc_position);    
alert(pos_x);
// alerts: NaN

// this syntax because a jQuery plug-in
var getScalePercent = function (value, perc) {
    return value * perc / 100;
}

// settings.mouse.x == 102 basically an integer
// getScalePercent(...) == 10 or 12.0390394028 basically an number

alert(settings.mouse.x + ' (' + typeof settings.mouse.x + ') --- ' + getScalePercent(((bg_w - a_w)/2), settings.bg.perc_position) + '(' + typeof getScalePercent(((bg_w - a_w)/2), settings.bg.perc_position) + ')');
// returns 102 (number) --- 12.000340563 (number)

我哪里错了?

1 个答案:

答案 0 :(得分:3)

我的猜测是getScalePercent正在返回NaN。由于typeof NaN正在返回number。检查功能是否符合您的预期。