gpt4 book ai didi

javascript - 无穷大与 Number.POSITIVE_INFINITY

转载 作者:行者123 更新时间:2023-12-03 12:50:27 25 4
gpt4 key购买 nike

我了解 Number.POSITIVE_INFINITY值为 Infinity , 和 Number.NEGATIVE_INFINITY值为 -Infinity .

我会使用 Number.POSITIVE_INFINITY 有什么原因吗?而不是 Infinity , 或 Number.NEGATIVE_INFINITY而不是 -Infinity ?

在相关说明中,isFinite 是否存在跨浏览器问题? ?

最佳答案

TL;博士Infinity曾经是可覆盖的; Number.POSITIVE_INFINITYNumber.NEGATIVE_INFINITY一直是只读的。
Infinity是全局对象的一个​​属性(window 是在浏览器中运行的 Javascript 的全局对象),而 Number.POSITIVE_INFINITYNumber 的属性构造函数。
在 ECMAScript 第 5 版之前,可以覆盖全局对象的值属性:

Infinity = 123;
Infinity; // 123
这同样适用于 undefinedNaN ,它们也是全局对象的属性,过去是可覆盖的。 Number 的属性构造函数一直是只读的:
Number.POSITIVE_INFINITY = 123;
Number.POSITIVE_INFINITY; // Infinity

眼镜:
ECMAScript 1st Edition (1997 年 6 月)

15.1.1.2 Infinity

The initial value of Infinity is +∞.

15.7.3.6 Number.POSITIVE_INFINITY

The value of Number.POSITIVE_INFINITY is +∞.

This property shall have the attributes { DontEnum, DontDelete, ReadOnly }.


ECMAScript 5th Edition (2009 年 12 月)
在 ES5 中,全局对象的值属性是只读的:

15.1.1.2 Infinity

The value of Infinity is +∞ (see 8.5).

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

Number 的属性构造函数并没有真正改变,但属性被重命名:

15.7.3.6 Number.POSITIVE_INFINITY

The value of Number.POSITIVE_INFINITY is +∞.

This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.


截至 ES2018,这些定义没有改变。

关于 isFinite :
我曾经发布过 question至于为什么 Google Closure 库为 isFinite 实现了自定义函数,答案是可能存在一些跨浏览器的不一致,尽管尚不清楚哪个浏览器和哪个不一致。

关于javascript - 无穷大与 Number.POSITIVE_INFINITY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8522400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com