为什么在isFinite()之后检查!isNaN()? [英] Why check for !isNaN() after isFinite()?
问题描述
我在 goog.math.isFiniteNumber 函数bc / 4 / trunk / closure / goog / docs / closure_goog_math_math.js.htmlrel =noreferrer> Google Closure Library 。它的作用是检查一个给定的数字是否都是有限的而不是 NaN
。
I came across the goog.math.isFiniteNumber
function in the Google Closure Library. What it does is checking whether a given number is both finite and not NaN
.
底层代码是:
goog.math.isFiniteNumber = function(num) {
return isFinite(num) && !isNaN(num);
};
首先,它使用原生 isFinite <检查数字是否有限/ code>函数,然后进行额外检查以确保该数字不是
NaN
使用 isNaN
。
So, first it checks whether the number is finite using the native isFinite
function, and then does an additional check to make sure the number isn't NaN
using isNaN
.
但是, isFinite
在参数为 NaN 的情况下已经返回false
。那么,检查 isNaN
提供哪些优势?
However, isFinite
already returns false in case the argument is NaN
. So, what advantages does the check for isNaN
provide?
推荐答案
如果 isFinite
按照 isFiniteNumber
的方式工作,那么就没有理由写isFiniteNumber。那里可能有一些浏览器将NaN视为有限的。
If isFinite
worked the way isFiniteNumber
did, then there would be no reason to write isFiniteNumber. There's probably some browser out there somewhere that treats NaN as finite.
这篇关于为什么在isFinite()之后检查!isNaN()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!