我只在Firefox的JavaScript控制台中尝试过,但以下语句都没有返回true:
parseFloat('geoff') == NaN;
parseFloat('geoff') == Number.NaN;
我只在Firefox的JavaScript控制台中尝试过,但以下语句都没有返回true:
parseFloat('geoff') == NaN;
parseFloat('geoff') == Number.NaN;
当前回答
简单的解决方案!
真的超级简单!这里!有这个方法!
function isReallyNaN(a) { return a !== a; };
使用简单如下:
if (!isReallyNaN(value)) { return doingStuff; }
参见此处使用此func vs选定答案的性能测试 另外:请参阅下面的第一个示例,了解两个替代实现。
例子:
function isReallyNaN(a) { return a !== a; }; var example = { 'NaN': NaN, 'an empty Objet': {}, 'a parse to NaN': parseFloat('$5.32'), 'a non-empty Objet': { a: 1, b: 2 }, 'an empty Array': [], 'a semi-passed parse': parseInt('5a5'), 'a non-empty Array': [ 'a', 'b', 'c' ], 'Math to NaN': Math.log(-1), 'an undefined object': undefined } for (x in example) { var answer = isReallyNaN(example[x]), strAnswer = answer.toString(); $("table").append($("<tr />", { "class": strAnswer }).append($("<th />", { html: x }), $("<td />", { html: strAnswer }))) }; table { border-collapse: collapse; } th, td { border: 1px solid; padding: 2px 5px; } .true { color: red; } .false { color: green; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <table></table>
如果您不想使用交替命名的方法,并希望确保它具有更全局的可用性,则可以采用两种实现路径。警告:这些解决方案涉及修改本机对象,可能不是您的最佳解决方案。请始终谨慎使用并注意您可能使用的其他库可能依赖于本机代码或类似的更改。
替代实现1:替换本地isNaN方法。
// Extremely simple. Just simply write the method.
window.isNaN = function(a) { return a !==a; }
替代实现2:附加到数字对象*建议,因为它也是ECMA 5到6的polyfill
Number['isNaN'] || (Number.isNaN = function(a) { return a !== a });
// Use as simple as
Number.isNaN(NaN)
如果为空,则进行备用溶液测试
我写了一个简单的窗口方法来测试对象是否为空。这有点不同,因为它没有给出如果项目是“确切”NaN,但我想我会抛出这个,因为它也可能是有用的,当寻找空项目。
/** isEmpty(varried)
* Simple method for testing if item is "empty"
**/
;(function() {
function isEmpty(a) { return (!a || 0 >= a) || ("object" == typeof a && /\{\}|\[(null(,)*)*\]/.test(JSON.stringify(a))); };
window.hasOwnProperty("empty")||(window.empty=isEmpty);
})();
例子:
;(function() { function isEmpty(a) { return !a || void 0 === a || a !== a || 0 >= a || "object" == typeof a && /\{\}|\[(null(,)*)*\]/.test(JSON.stringify(a)); }; window.hasOwnProperty("empty")||(window.empty=isEmpty); })(); var example = { 'NaN': NaN, 'an empty Objet': {}, 'a parse to NaN': parseFloat('$5.32'), 'a non-empty Objet': { a: 1, b: 2 }, 'an empty Array': new Array(), 'an empty Array w/ 9 len': new Array(9), 'a semi-passed parse': parseInt('5a5'), 'a non-empty Array': [ 'a', 'b', 'c' ], 'Math to NaN': Math.log(-1), 'an undefined object': undefined } for (x in example) { var answer = empty(example[x]), strAnswer = answer.toString(); $("#t1").append( $("<tr />", { "class": strAnswer }).append( $("<th />", { html: x }), $("<td />", { html: strAnswer.toUpperCase() }) ) ) }; function isReallyNaN(a) { return a !== a; }; for(x in example){var answer=isReallyNaN(example[x]),strAnswer=answer.toString();$("#t2").append($("<tr />",{"class":strAnswer}).append($("<th />",{html:x}),$("<td />",{html:strAnswer.toUpperCase()})))}; table { border-collapse: collapse; float: left; } th, td { border: 1px solid; padding: 2px 5px; } .true { color: red; } .false { color: green; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <table id="t1"><thead><tr><th colspan="2">isEmpty()</th></tr></thead><thead><tr><th>Value Type</th><th>Bool Return</th></tr></thead></table> <table id="t2"><thead><tr><th colspan="2">isReallyNaN()</th></tr></thead><thead><tr><th>Value Type</th><th>Bool Return</th></tr></thead></table>
极深检查是否为空
最后一个有点深,甚至检查一个对象是否充满了空白对象。我相信它有改进的空间,也有可能出现漏洞,但到目前为止,它似乎能捕捉到几乎所有的东西。
function isEmpty(a) { if (!a || 0 >= a) return !0; if ("object" == typeof a) { var b = JSON.stringify(a).replace(/"[^"]*":(0|"0*"|false|null|\{\}|\[(null(,)?)*\]),?/g, '').replace(/"[^"]*":\{\},?/g, ''); if ( /^$|\{\}|\[\]/.test(b) ) return !0; else if (a instanceof Array) { b = b.replace(/(0|"0*"|false|null|\{\}|\[(null(,)?)*\]),?/g, ''); if ( /^$|\{\}|\[\]/.test(b) ) return !0; } } return false; } window.hasOwnProperty("empty")||(window.empty=isEmpty); var example = { 'NaN': NaN, 'an empty Objet': {}, 'a parse to NaN': parseFloat('$5.32'), 'a non-empty Objet': { a: 1, b: 2 }, 'an empty Array': new Array(), 'an empty Array w/ 9 len': new Array(9), 'a semi-passed parse': parseInt('5a5'), 'a non-empty Array': [ 'a', 'b', 'c' ], 'Math to NaN': Math.log(-1), 'an undefined object': undefined, 'Object Full of Empty Items': { 1: '', 2: [], 3: {}, 4: false, 5:new Array(3), 6: NaN, 7: null, 8: void 0, 9: 0, 10: '0', 11: { 6: NaN, 7: null, 8: void 0 } }, 'Array Full of Empty Items': ["",[],{},false,[null,null,null],null,null,null,0,"0",{"6":null,"7":null}] } for (x in example) { var answer = empty(example[x]), strAnswer = answer.toString(); $("#t1").append( $("<tr />", { "class": strAnswer }).append( $("<th />", { html: x }), $("<td />", { html: strAnswer.toUpperCase() }) ) ) }; function isReallyNaN(a) { return a !== a; }; for(x in example){var answer=isReallyNaN(example[x]),strAnswer=answer.toString();$("#t2").append($("<tr />",{"class":strAnswer}).append($("<th />",{html:x}),$("<td />",{html:strAnswer.toUpperCase()})))}; table { border-collapse: collapse; float: left; } th, td { border: 1px solid; padding: 2px 5px; } .true { color: red; } .false { color: green; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <table id="t1"><thead><tr><th colspan="2">isEmpty()</th></tr></thead><thead><tr><th>Value Type</th><th>Bool Return</th></tr></thead></table> <table id="t2"><thead><tr><th colspan="2">isReallyNaN()</th></tr></thead><thead><tr><th>Value Type</th><th>Bool Return</th></tr></thead></table>
其他回答
MDN的parseFloat页面中提到了另一个解决方案
它提供了一个过滤器函数来执行严格的解析
var filterFloat = function (value) {
if(/^(\-|\+)?([0-9]+(\.[0-9]+)?|Infinity)$/
.test(value))
return Number(value);
return NaN;
}
console.log(filterFloat('421')); // 421
console.log(filterFloat('-421')); // -421
console.log(filterFloat('+421')); // 421
console.log(filterFloat('Infinity')); // Infinity
console.log(filterFloat('1.61803398875')); // 1.61803398875
console.log(filterFloat('421e+0')); // NaN
console.log(filterFloat('421hop')); // NaN
console.log(filterFloat('hop1.61803398875')); // NaN
然后你可以用isNaN来检查它是否是NaN
我创建了这个小函数,它就像一个咒语。 而不是检查NaN,这似乎是违反直觉的,你检查一个数字。我很确定我不是第一个这样做的人,但我想分享一下。
function isNum(val){
var absVal = Math.abs(val);
var retval = false;
if((absVal-absVal) == 0){
retval = true
}
return retval;
}
规则是:
NaN != NaN
isNaN()函数的问题是,在某些情况下,它可能会返回意想不到的结果:
isNaN('Hello') //true
isNaN('2005/12/12') //true
isNaN(undefined) //true
isNaN('NaN') //true
isNaN(NaN) //true
isNaN(0 / 0) //true
检查该值是否真的为NaN的更好方法是:
function is_nan(value) {
return value != value
}
is_nan(parseFloat("geoff"))
从ES6开始,Object.is(..)是一个新的实用程序,可以用来测试两个值是否绝对相等:
var a = 3 / 'bar';
Object.is(a, NaN); // true
你应该使用全局isNaN(value)函数调用,因为:
支持跨浏览器 有关文档,请参阅isNaN
例子:
isNaN('geoff'); // true
isNaN('3'); // false
我希望这对你有所帮助。