Number.isNaN doesn't exist in IE

♀尐吖头ヾ 提交于 2019-12-06 17:27:11

问题


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

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).

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.

May be there is some alternative function exists?


回答1:


Why does not IE support the Number.isNaN function?

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.

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;
}



回答2:


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

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://stackoverflow.com/questions/26962341/number-isnan-doesnt-exist-in-ie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!