admin管理员组文章数量:1401143
I am looking at JavaScript's number type system.
I'm using Chrome, When I evaluate 15--
for a number literal I get a ReferenceError
since it makes no sense to decrement a constant.
When I evaluate var x=10;x--;
as expected everything works.
Expectantly var a=Infinity;a--
evaluates to Infinity
, this all makes sense and is in accordance to the javascript language spec.
However to my surprise Infinity--
and Infinity++
evaluate to Infinity
unlike other literals.
This also happens for Number.POSITIVE_INFINITY
which is the same.
tl;dr :
Why does Infinity--
yield infinity as a result when 15--
and (new Number(15))--
yield a reference error?
I am looking at JavaScript's number type system.
I'm using Chrome, When I evaluate 15--
for a number literal I get a ReferenceError
since it makes no sense to decrement a constant.
When I evaluate var x=10;x--;
as expected everything works.
Expectantly var a=Infinity;a--
evaluates to Infinity
, this all makes sense and is in accordance to the javascript language spec.
However to my surprise Infinity--
and Infinity++
evaluate to Infinity
unlike other literals.
This also happens for Number.POSITIVE_INFINITY
which is the same.
tl;dr :
Why does Infinity--
yield infinity as a result when 15--
and (new Number(15))--
yield a reference error?
2 Answers
Reset to default 11Infinity
as used in your example is not actually a value but refers to the 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
}.
So, Infinity--
is the same as window.Infinity--
which is perfectly valid.
Because there is no such thing as a the number infinity, it is a concept, and thus in coding it isn't built as other constants but as an object like null or undefined but with some properties thrown in to make it behave nice with Math methods.
本文标签: Javascript Infinity (infinity minus minus) yields InfinityStack Overflow
版权声明:本文标题:Javascript Infinity-- (infinity minus minus) yields Infinity - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744262317a2597778.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论