【发布时间】:2020-05-08 19:40:04
【问题描述】:
为什么函数 isNaN() 在传递 BigInt 作为参数时会抛出错误?
错误:
Uncaught TypeError: Cannot convert a BigInt value to a number
at isNaN (<anonymous>)
at <anonymous>:1:1
要复制的代码:
let i = BigInt('2');
isNaN(i);
【问题讨论】:
-
根据文档 BigInt 是一个对象。它不是一个数字。
-
BigInt不能被强制转换为数字,这是isNaN所做的第一件事。见the spec on isNaN 和the spec on ToNumber -
因为它是这样指定的:
isNaN()->ToNumber()->BigInt-> “引发 TypeError 异常” -
@Amy "BigInt cannot be coerced into a number" - 诚然有点迂腐,但我相信它可以,只是不使用这种方法: ) 例如,
Number(BigInt('2')) === 2工作正常。 -
@Andreas 正确!这是我评论的基础。
标签: javascript bigint