JavaScript bitwise undefined pitfalls?

痴心易碎 提交于 2019-12-07 06:07:33

问题


What is the logic of bitwise operators on undefined???

var x;
console.log(x);     // undefined
console.log(x^7);   // 7
console.log(7^x);   // 7
console.log(x|7);   // 7
console.log(7|x);   // 7
console.log(7&x);   // 0
console.log(x&7);   // 0
console.log(~x);    // -1
console.log(x*2);   // NaN
console.log(x/2);   // NaN
console.log(x+2);   // NaN
console.log(x-2);   // NaN

I can see some sense in NaN. Because undefined -2 is really 'not a number'. But I do not follow any logic on bitwise operators and undefined.


回答1:


The internal function [ToInt32] is called on all operands for all bitwise operators.

Note that ToInt32(undefined) -> 0 and the range is [0, 2^32)



来源:https://stackoverflow.com/questions/16369280/javascript-bitwise-undefined-pitfalls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!