为什么``+ 0n`丢弃错误,但`-0n`没有呢?
以下代码在JavaScript中引发错误:
console.log(String(+0n))
但是此代码成功运行:
console.log(String(-0n))
为什么+0n
引发错误,但 -0N
却没有?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这样它就不会打破ASM.JS < /a>:
AS bergi 在评论中突出显示,这是三个选项中最不好的:
+
bigint-- &gt; bigint:打破ASM.JS,以及其他任何使假设“ Unary Plus给出数字” 的事物;+
bigint-&gt;编号:与不允许在数字和bigint之间进行隐式转换;或+
bigint-&gt;错误。So that it doesn't break asm.js:
As Bergi highlights in the comments, this was the least bad of three options:
+
BigInt -> BigInt: breaks asm.js, and anything else that made the assumption "unary plus gives a Number";+
BigInt -> Number: conflicts with the design decision to disallow implicit conversions between Number and BigInt; or+
BigInt -> error.+0n
被视为+(BigInt(0))
,因为Unary+
表示“铸造到整数”,并且无法自动自动这样做(由于某种原因)-0N
被视为bigint(-0)
,因为负数可以是大整数(您需要检查您的控制台,因为我猜这是一个堆栈尼珀的错误,阻止bigint被施放到
console.log call
中的字符串)+0n
is treated as+(BigInt(0))
, since unary+
means "cast to integer", and it can't automatically do that (for some reason)-0n
is treated asBigInt(-0)
, since negative numbers can be big integers(You need to check your console for this, since I guess there's a bug in the StackSnippets preventing BigInts from being casted to a string in the
console.log call
)