为什么 (~(1 << 31)) === ~(1 << 31) 但在转换为字符串时它们不相等?
Why does (~(1 << 31)) === ~(1 << 31) yet they aren't equal when converted to strings?
在 JS 中:
(~(1 << 31)) === ~(1 << 31)
> true
(~(1 << 31)).toString(2) === ~(1 << 31).toString(2)
> false
这怎么可能?我以为 ===
运算符是相同实体之间的严格比较?
这是因为表达式的计算方式如下:
(~(1 << 31)).toString(2) === ~((1 << 31).toString(2))
//----------------------------^ see the parenthesis
事实证明,由于 JS 中的操作顺序,我没有比较确切的值:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
成员访问 .toString()
优先于按位运算符。哎呀!
在 JS 中:
(~(1 << 31)) === ~(1 << 31)
> true
(~(1 << 31)).toString(2) === ~(1 << 31).toString(2)
> false
这怎么可能?我以为 ===
运算符是相同实体之间的严格比较?
这是因为表达式的计算方式如下:
(~(1 << 31)).toString(2) === ~((1 << 31).toString(2))
//----------------------------^ see the parenthesis
事实证明,由于 JS 中的操作顺序,我没有比较确切的值:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
成员访问 .toString()
优先于按位运算符。哎呀!