我正在研究 JavaScript 的数字类型系统。
我正在使用 Chrome,当我为一个数字文字计算 15--
时,我得到了一个 ReferenceError
,因为递减一个常量是没有意义的。
当我按预期评估 var x=10;x--;
时,一切正常。
预计 var a=Infinity;a--
的计算结果为 Infinity
,这一切都有意义并且符合 to the javascript language spec .
然而,令我惊讶的是,Infinity--
和 Infinity++
的计算结果与其他文字不同,Infinity
。
Number.POSITIVE_INFINITY
也是如此。
tl;dr :
当 15--
和 (new Number(15))--
产生一个结果时,为什么 Infinity--
产生无穷大引用错误?
最佳答案
Infinity
在您的示例中使用的实际上不是一个值,而是指 Infinity
property of the global object :
15.1 The Global Object
[...]
15.1.1 Value Properties of the Global Object
[...]
15.1.1.2 Infinity
The value ofInfinity
is+∞
(see 8.5). This property has the attributes { [[Writable]]:false
, [[Enumerable]]:false
, [[Configurable]]:false
}.
因此,Infinity--
与完全有效的 window.Infinity--
相同。
关于Javascript Infinity-- (infinity minus minus) 产生无穷大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14777757/