为什么typeof null“object”? [英] why is typeof null "object"?

查看:180
本文介绍了为什么typeof null“object”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读'针对Web开发人员的专业Javascript'第4章,它告诉我五种类型的原语是:undefined,null,boolean,number和string。

I am reading 'Professional Javascript for Web Developers' Chapter 4 and it tells me that the five types of primitives are: undefined, null, boolean, number and string.

如果 null 是原语,为什么 typeof(null)返回object

If null is a primitive, why does typeof(null) return "object"?

这不意味着 null 通过引用传递(我' m假设这里所有对象都通过引用传递),因此使它不是原始的?

Wouldn't that mean that null is passed by reference (I'm assuming here all objects are passed by reference), hence making it NOT a primitive?

推荐答案

在JavaScript的第一个实现中, JavaScript值表示为类型标记和值,对象的类型标记为 0 null 为在大多数平台上表示为 NULL 指针( 0x00 )。因此,null将 0 作为类型标记,因此伪造 typeof 返回值(参考)。

In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value, with the type tag for objects being 0, and null was represented as the NULL pointer (0x00 on most platforms). As a result, null had 0 as a type tag, hence the bogus typeof return value (reference).

 typeof null === 'object'; // This stands since the beginning of JavaScript

为ECMAScript提出了修复(通过选择) -在)。这将导致:

A "fix" was proposed for ECMAScript (via an opt-in). It would have resulted in:

typeof null === 'null'

...但此更改是拒绝 ,由于代码使用此特定怪癖来测试 null

... but this change was rejected, due to issues with code using this specific "quirk" to test for null.

这篇关于为什么typeof null“object”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆