为什么`isFinite(null) === true`?

编程入门 行业动态 更新时间:2024-10-25 19:32:34
本文介绍了为什么`isFinite(null) === true`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下是我觉得有意义的例子.

The following are examples that make sense to me.

isFinite(5) // true - makes sense to me, it is a number and it is finite typeof 5 // "number" isFinite(Infinity) // false - makes sense for logical reasons typeof Infinity // "number" isFinite(document) // false - makes sense as well, it's not even a number typeof document // "object"

以下是我感到困惑的地方.

The following is where I get confused.

isFinite(null) // true - Wait what? Other non-number objects returned false. I see no reason? typeof null // "object"

我只是不明白这背后的原因.我想要的是尽可能低级的答案.我认为 null 正在转换为 0,为什么?这还有什么其他影响?

I just don't see the reasoning behind this. What I'd like is the most low-level answer possible. I think null is being converted to 0, why? What other impacts does this have?

推荐答案

ECMAScript 规范 (5.1) 将 isFinite 定义为:

The ECMAScript spec (5.1) defines isFinite to act as such:

isFinite(数字)

isFinite (number)

如果参数强制为 NaN、+∞ 或 −∞,则返回 false,否则返回 true.

Returns false if the argument coerces to NaN, +∞, or −∞, and otherwise returns true.

如果 ToNumber(number) 是 NaN、+∞ 或 −∞,则返回 false.

If ToNumber(number) is NaN, +∞, or −∞, return false.

否则返回真.

换句话说,isFinite 正在调用 ToNumber 在传入的任何内容上,然后将其与 pos/neg 无穷大或 NaN 进行比较.

In other words, isFinite is calling ToNumber on whatever's passed in, and then comparing it to either pos/neg infinity or NaN.

在 JavaScript 中(注意使用 != 而不是更常见的 !==,导致类型转换):

In JavaScript (note the use of != instead of the more common !==, causing the type cast):

function isFinite(someInput) { return !isNaN(someInput) && someInput != Number.POSITIVE_INFINITY && someInput != Number.NEGATIVE_INFINITY; }

(如下面的评论中所述,someInput != NaN 不是必需的,因为 NaN 被定义为不等同于一切,包括它自己.)

(As noted in the comments below, someInput != NaN is not needed, as NaN is defined to not be equivalent to everything, including itself.)

现在,为什么 null 被转换为零(而不是 undefined)?正如 TylerH 在评论中所说,null 意味着一个值存在,但为空.它的数学表示是 0.undefined 意味着那里没有值,所以当我们尝试调用 ToNumber 时我们得到 NaN

Now, why is null converted to zero (as opposed to undefined)? As TylerH says in the comments, null means that a value exists, but is empty. The mathematical representation of this is 0. undefined means that there isn't a value there, so we get NaN when trying to call ToNumber on it.

www.ecma-international/ecma-262/5.1/#sec-15.1.2.5

然而,ECMAScript 6 带来了一个非转换的 isFinite 作为 Number 的属性.Douglas Crockford 在这里建议:wiki.ecmascript/doku.php?id=harmony:number.isfinite

However, ECMAScript 6 is bringing along a non-converting isFinite as a property of Number. Douglas Crockford suggested it here: wiki.ecmascript/doku.php?id=harmony:number.isfinite

更多推荐

为什么`isFinite(null) === true`?

本文发布于:2023-11-24 21:21:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626880.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:isFinite   null   true

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!