admin管理员组

文章数量:1317909

Taken from the underscore.js source:

_.isNaN = function(obj) {
  return _.isNumber(obj) && obj != +obj;
};

Why did they do it this way? Is the above implementation equivalent to:

_.isNaN = function(obj) {
  return obj !== obj;
};

If it is, why the "more plicated" version? If it is not, what are the behavioural differences?

Taken from the underscore.js source:

_.isNaN = function(obj) {
  return _.isNumber(obj) && obj != +obj;
};

Why did they do it this way? Is the above implementation equivalent to:

_.isNaN = function(obj) {
  return obj !== obj;
};

If it is, why the "more plicated" version? If it is not, what are the behavioural differences?

Share Improve this question edited Mar 4, 2013 at 12:14 Denys Séguret 383k90 gold badges810 silver badges775 bronze badges asked Mar 2, 2013 at 15:22 RandomblueRandomblue 116k150 gold badges362 silver badges557 bronze badges 7
  • Or even simpler, the native isNaN function... – Niet the Dark Absol Commented Mar 2, 2013 at 15:24
  • 2 @Kolink taken from the Underscore docs: "Note: this is not the same as the native isNaN function, which will also return true if the variable is undefined" – robertklep Commented Mar 2, 2013 at 15:26
  • 1 @Kolink isNaN(undefined) gives true. – Denys Séguret Commented Mar 2, 2013 at 15:27
  • 1 Just like isNaN("NaN")... – Denys Séguret Commented Mar 2, 2013 at 15:28
  • Is the purpose of the "is not a number" function not to find out if a variable is not a number? – Niet the Dark Absol Commented Mar 2, 2013 at 15:32
 |  Show 2 more ments

4 Answers 4

Reset to default 4

_.isNaN(new Number(NaN)) returns true.

And that's by design.

var n = new Number(NaN);
console.log(_.isNaN(n), n!==n); // logs true, false

The host environment (e.g. web-browser environment) may introduce other values that aren't equal to themselves. The _.isNumber(obj) part makes sure that the input is a Number value, so that _.isNaN only returns true if the NaN value is passed.

If + is given before of any value with no preceding value to + , JavaScript engine will try to convert that variable to Number.If it is valid it will give you the Number else it will return NaN. For example

 + "1" // is equal to integer value 1
 + "a1" // will be NaN because "a1" is not a valid number

In above case

 +"a1" != "a1" // true, so this is not a number, one case is satisfied
 +"1" == "1" // true, so it is number

Another simple case would be, why the below expression give this output

 console.log("Why I am " + typeof + "");
 // returns "Why I am number"

Because +"" is 0.

If you want to check whether it is a number or not you could use the below function

function isNumber(a){ 
   /* first method : */  return (+a == a);
   /* second method : */  return (+(+a) >= 0);
   // And so many other exists
}

Someone correct me if I am wrong somewhere..

I found one case for _.isNaN It shows different result with the native one if you pass there an object

_.isNaN({}) => false
//but
isNaN({}) => true

本文标签: javascriptUnderstanding underscore39s implementation of isNaNStack Overflow