首先,我们应该知道:
typeof null === "object;//true;
但这是为什么呢?在网上查找资料有这样的答案:
Because the initial version of JS was based on 32-bit systems, which stored type information of variables in the lower bits for performance considerations. Those start with 000 are objects, and all the bits of null are zero, so it is erroneously treated as an object. Although the current code of checking internal types has changed, this bug has been passed down.
由此我们可以猜想:是不是所有的基本数据类型的存储地址具有一定的规律性呢?如果是这样的话,那么typeof 操作符执行的本质是不是查找基本数据类型值的地址来判断该变量是什么类型的呢?(因为栈的大小是有限的)
如果不是这样,那是如何确定变量的数据类型的?
本文探讨了JavaScript中null被typeof操作符识别为object的原因。源于JS初期基于32位系统,变量类型信息存储于低位以提升性能。所有以000开头的被视为object,而null的所有位都是0,故此误判沿袭至今。
343

被折叠的 条评论
为什么被折叠?



