javascript 为什么 typeof 为空“对象”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18808226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:18:03  来源:igfitidea点击:

Why is typeof null "object"?

javascriptprimitive

提问by thetrystero

I'm 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.

我正在阅读“Web 开发人员的专业 Javascript”第 4 章,它告诉我五种类型的原语是:未定义、空值、布尔值、数字和字符串。

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

如果null是原始类型,为什么typeof(null)返回"object"

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

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

回答by Dipak Ingole

From the MDN page about the behaviour of the typeofoperator:

关于typeof操作员行为的 MDN 页面

null

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

In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value. The type tag for objects was 0. nullwas represented as the NULL pointer (0x00 in most platforms). Consequently, null had 0 as type tag, hence the "object" typeofreturn value. (reference)

A fix was proposed for ECMAScript (via an opt-in), but was rejected. It would have resulted in typeof null === 'null'.

null

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

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

提出了针对 ECMAScript 的修复(通过选择加入),但被拒绝。它会导致typeof null === 'null'.

回答by Matt Ball

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

如果null是原始类型,为什么typeof(null)返回"object"

Because the spec says so.

因为规范是这么说的

11.4.3The typeofOperator

The production UnaryExpression: typeofUnaryExpressionis evaluated as follows:

  1. Let valbe the result of evaluating UnaryExpression.
  2. If Type(val) is Reference, then
       a. If IsUnresolvableReference(val) is true, return "undefined".
       b. Let valbe GetValue(val).
  3. Return a String determined by Type(val) according to Table 20.

enter image description here

11.4.3typeof操作

产生式UnaryExpression: typeofUnaryExpression的计算方式如下:

  1. val为评估UnaryExpression的结果。
  2. 如果Type( val) 是Reference,则
       a. 如果IsUnresolvableReference( val) 为,则返回“ undefined”。
       湾 令valGetValue( val)。
  3. 根据表 20返回由类型( val)确定的字符串。

在此处输入图片说明

回答by chryss

As has been pointed out, the spec says so. But since the implementation of JavaScript predates the writing of the ECMAScript spec, and the specification was careful not to correct foibles of the initial implementation, there's still a legitimate question about why it was done this way in the first place. Douglas Crockford calls it a mistake. Kiro Risk thinks it kinda sorta makes sense:

正如已经指出的那样,规范是这样说的。但是由于 JavaScript 的实现早于 ECMAScript 规范的编写,并且规范小心翼翼地不纠正初始实现的弱点,所以仍然存在一个合理的问题,即为什么首先要这样做。道格拉斯·克罗克福德称这是一个错误。Kiro Risk认为这有点道理

The reasoning behind this is that null, in contrast with undefined, was (and still is) often used where objects appear. In other words, nullis often used to signify an empty reference to an object. When Brendan Eich created JavaScript, he followed the same paradigm, and it made sense (arguably) to return "object". In fact, the ECMAScript specification defines nullas the primitive value that represents the intentional absence of any object value(ECMA-262, 11.4.11).

这背后的原因是null,与 相比undefined,曾经(现在仍然)经常在对象出现的地方使用。换句话说,null通常用于表示对对象的空引用。当 Brendan Eich 创建 JavaScript 时,他遵循相同的范式,并且(可以说)返回“对象”是有意义的。事实上,ECMAScript 规范将原始值定义null表示有意不存在任何对象值的原始值(ECMA-262, 11.4.11)。

回答by Faisal Ashfaq

From the book YDKJS

来自YDKJS一书

This is a long-standing bug in JS, but one that is likely never going to be fixed. Too much code on the Web relies on the bug and thus fixing it would cause a lot more bugs!

这是 JS 中的一个长期存在的错误,但可能永远不会被修复。网络上太多的代码依赖于这个错误,因此修复它会导致更多的错误!

回答by jobin

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

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

in short: it is bug in ECMAScript, and the type should be null

简而言之:这是 ECMAScript 中的错误,类型应该是 null

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

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

回答by Farzana Khan

In JavaScript null is "nothing". It is supposed to be something that doesn't exist. Unfortunately, in JavaScript, the data type of null is an object. You can consider it a bug in JavaScript that typeof null is an object. It should be null.

在 JavaScript 中 null 是“无”。它应该是不存在的东西。不幸的是,在 JavaScript 中,null 的数据类型是一个对象。您可以将其视为 JavaScript 中的一个错误,即 typeof null 是一个对象。它应该为空。

回答by Slim Coder

In JavaScript, typeof null is 'object', which incorrectly suggests that null is an object. This is a bug and one that unfortunately can't be fixed, because it would break existing code.

在 JavaScript 中,typeof null 是 'object',这错误地表明 null 是一个对象。这是一个错误,不幸的是无法修复,因为它会破坏现有代码。

回答by zabusa

for people who interested in some code that made this behaviour this is the link for you guyz.... why typeof null is object with the implimentation

对于那些对导致这种行为的代码感兴趣的人,这是给你们的链接…… 为什么 typeof null 是具有实现的对象