简体   繁体   English

当与typeof运算符一起使用时,javascript null返回对象

[英]javascript null returns object when used with typeof operator

Why does typeof return object when used with null ? 为什么typeofnull使用时会返回object From my understanding this is a top level property of the global object. 据我了解,这是全局对象的顶级属性。 References to official documentation would greatly help. 引用官方文档将大有帮助。

In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value. 在JavaScript的第一个实现中,JavaScript值表示为类型标记和值。 The type tag for objects was 0. null was represented as the NULL pointer (0x00 in most platforms). 对象的类型标记为0。null表示为NULL指针(在大多数平台中为0x00)。 Consequently, null had 0 as type tag, hence the bogus typeof return value. 因此,null具有0作为类型标记,因此是假的typeof返回值。 (reference) (参考)

A fix was proposed for ECMAScript (via an opt-in), but was rejected. 提出了针对ECMAScript的修复程序(通过选择加入),但被拒绝。 It would have resulted in typeof null === 'null'. 它将导致typeof null ==='null'。

Reference: 参考:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/typeof

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/null

Because the specification says so: 因为规范是这样说的:

Table 20 — typeof Operator Results

 Type of val     Result
---------------------------
  Undefined    "undefined"
  Null         "object"
  Boolean      "boolean"
  Number       "number"
  String       "string"
  ...

And it is also saying: 它也说:

4.3.11 null value 4.3.11空值

primitive value that represents the intentional absence of any object value 表示有意缺少任何对象值的原始值

Nevertheless, null is still of data type Null and you can use null where you can use other objects (eg accessing a property on null throws an error instead of returning undefined (for better or worse)). 不过, null仍然是Null 数据类型 ,您可以在可以使用其他对象的地方使用null (例如,访问null上的属性会引发错误,而不是返回undefined (无论好坏))。

From my understanding this is a top level property of the global object. 据我了解,这是全局对象的顶级属性。

No, null is literal , just like 5 is a number literal. 不, null文字 ,就像5是数字文字一样。 undefined on the other hand is a global variable (as if it wasn't already confusing enough). 另一方面, undefined是一个全局变量(好像还没有引起足够的混乱)。

It is . 这是 In Javascript null is an object 在Javascript中, null是一个object

Kiro Risk says 基罗风险说

The reasoning behind this is that null, in contrast with undefined, was (and still is) often used where objects appear. 其背后的原因是,与未定义相反,在对象出现的地方经常(并且仍然)使用null。 In other words, null is often used to signify an empty reference to an object. 换句话说,null通常用于表示对对象的空引用。 When Brendan Eich created JavaScript, he followed the same paradigm, and it made sense (arguably) to return "object". 当布伦丹·艾希(Brendan Eich)创建JavaScript时,他遵循相同的范例,并且可以说返回“对象”是合理的(可以说)。 In fact, the ECMAScript specification defines null as the primitive value that represents the intentional absence of any object value (ECMA-262, 11.4.11). 实际上,ECMAScript规范将null定义为表示有意没有任何对象值的原始值(ECMA-262,11.4.11)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM