为什么数字构造函数无法用分离器分析数字?
由于JavaScript具有数字分离器(_
,u+005F
),为什么numbers(“ 3_0”)
return> return nan
?它不应该像number(“ 0x08”)
返回8?
Number.isNaN(Number("3_0")) // true
Number.isNaN(Number(3_0)) // false
Number("3_0") === 30 // false
Number(3_0) === 30 // true
Since JavaScript has numeric separators (_
, U+005F
), why does Number("3_0")
return NaN
? Shouldn't it work like Number("0x08")
which returns 8?
Number.isNaN(Number("3_0")) // true
Number.isNaN(Number(3_0)) // false
Number("3_0") === 30 // false
Number(3_0) === 30 // true
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 spec spec spec a>,在转换为数字值时接受的数字文字的语法与字符串值接受的语法之间存在一些差异。
区别之一是
如果我不得不猜测,原因是接受此类字符将改变现有有效的JavaScript代码的行为,这可能会破坏工作应用程序。
According to the spec, there are a few differences between the syntax accepted for numeric literals and the syntax accepted for a string value when being converted to a numeric value.
One of the differences is
If I have to guess, the reason is that accepting such characters would change the behavior of existing valid JavaScript code, which may break working applications.