javascript Number.isNaN 在 IE 中不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26962341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:45:02  来源:igfitidea点击:

Number.isNaN doesn't exist in IE

javascriptinternet-explorernumbersnan

提问by impulsgraw

Why does not IE support the Number.isNaNfunction? I can't use simple isNan instead of Number.isNaN 'cause these functions are different! For example:

为什么 IE 不支持Number.isNaN函数?我不能使用简单的 isNan 而不是 Number.isNaN ,因为这些函数是不同的!例如:

Number.isNaN("abacada") != isNaN("abacada") //false != true

I need to abstract string and number and check, is some var really contains NaN(I mean NaN constant, but not-a-number value like strings).

我需要抽象字符串和数字并检查一些 var 是否真的包含NaN(我的意思是 NaN 常量,但不是像字符串那样的数字值)。

someobj.someopt = "blah"/0;
someobj.someopt2 = "blah";
someobj.someopt3 = 150;
for(a in someobj) if(Number.isNaN(someobj[a])) alert('!');

That code should show alert for 1 time. But if I will change Number.isNaN to isNaN, it will alert 2 times. There are differences.

该代码应显示警报 1 次。但是如果我将 Number.isNaN 更改为 isNaN,它会发出 2 次警报。有差异。

May be there is some alternative function exists?

可能存在一些替代功能吗?

回答by ruakh

Why does not IE support the Number.isNaNfunction?

为什么 IE 不支持Number.isNaN函数?

That's rather off-topic for Stack Overflow, but according to the MDN page for Number.isNaN, it's not defined in any standard — it's only in the draft spec for ECMAScript 6 — and Internet Explorer is not the only browser that doesn't support it.

这对于 Stack Overflow 来说是相当离题的,但根据的 MDN 页面Number.isNaN,它没有在任何标准中定义——它只在 ECMAScript 6 的草案规范中——并且 Internet Explorer 不是唯一不支持它的浏览器。

May be there is some alternative function exists?

可能存在一些替代功能吗?

Not really, but you can easily define one:

并非如此,但您可以轻松定义一个:

function myIsNaN(o) {
    return typeof(o) === 'number' && isNaN(o);
}

or if you prefer:

或者,如果您更喜欢:

function myIsNaN(o) {
    return o !== o;
}

回答by ?ukasz Wojtanowski

You need use isNaN without "Number." Like this:

您需要使用不带“数字”的 isNaN。像这样:

for(a in someobj) if(isNaN(someobj[a])) alert('!');

for(a in someobj) if(isNaN(someobj[a])) alert('!');

This also work fine for chrome.

这也适用于铬。

You can find more on microsoft site https://docs.microsoft.com/en-us/scripting/javascript/reference/isnan-function-javascript

您可以在微软网站https://docs.microsoft.com/en-us/scripting/javascript/reference/isnan-function-javascript上找到更多信息