- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我知道“null”是一种“对象”
但是,当我执行 Object.getProtoTypeOf(null) 时,我收到错误。 instanceof 也是如此,这有点相同。
如果 null 是一种 Object 类型...它应该在它的原型(prototype)链中,对吧?
// 'object'
typeof null;
// gives error - Cannot convert undefined or null to object
Object.getPrototypeOf(null);
// false ---- why?
null instanceof Object
是我漏掉了什么,还是 JS 太嬉皮了?!
最佳答案
看,null
不是一个对象。确实,typeof null
的计算结果为 'object'
,但它实际上来自 JavaScript 的一个限制,并且可能被认为是一个怪癖 - 在某种程度上是一个错误。引用this article :
The "
typeof null
" bug is a remnant from the first version of JavaScript. In this version, values were stored in 32 bit units, which consisted of a small type tag (1–3 bits) and the actual data of the value. The type tags were stored in the lower bits of the units. There were five of them:
- 000:
object
. The data is a reference to an object.- 1:
int
. The data is a 31 bit signed integer.- 010:
double
. The data is a reference to a double floating point number.- 100:
string
. The data is a reference to a string.- 110:
boolean
. The data is a boolean.That is, the lowest bit was either one, then the type tag was only one bit long. Or it was zero, then the type tag was three bits in length, providing two additional bits, for four types. Two values were special:
undefined
(JSVAL_VOID) was the integer −230 (a number outside the integer range).null
(JSVAL_NULL) was the machine code NULL pointer. Or: an object type tag plus a reference that is zero.It should now be obvious why typeof thought that null was an object: it examined its type tag and the type tag said “object”.
现在,对于其余操作,null
被视为原始值。这就是为什么,例如,null instanceof Object
返回 false - 任何基元都会返回 false,因为它不能是实例。将 42 instanceof Number
(false) 与 typeof 42 === 'number'
进行比较。
对于Object.getPrototypeOf(null)
,它有点不同。在 ES5 standard如果该函数的参数是原语 - 任何原语,则该函数应该抛出异常:
When the
getPrototypeOf
function is called with argumentO
, the following steps are taken:
- If
Type(O)
is notObject
throw aTypeError
exception.- Return the value of the
[[Prototype]]
internal property ofO
.
但是,在 ES2015 中,它们 changed it :
When the
getPrototypeOf
function is called with argumentO
, the following steps are taken:
- Let
obj
beToObject(O)
.ReturnIfAbrupt(obj)
.- Return
obj.[[GetPrototypeOf]]()
.
...所以 Object.getPrototypeOf(42)
现在可以工作了。然而 Object.getPrototypeOf(undefined)
和 Object.getPrototypeOf(null)
都会抛出相同的错误:
Uncaught TypeError: Cannot convert undefined or null to object
此异常是由 ToObject()
方法 ( doc ) 引发的 - 这是有道理的,因为不能“装箱” null
和 undefined
。在某些方面,它与尝试访问 null
或 undefined
属性时遇到的错误类似。
关于javascript - 为什么 getPrototypeOf 'null' 会出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42855464/
对于下面的代码,我想知道为什么 Object.getPrototypeOf(person) 可以工作,但 person.getPrototypeOf(person) 不起作用?我认为规则是:如果对象没
在this video (大约 31 分钟),Crockford 说他们(代表 ECMAScript 委员会发言)建议不要使用Object.getPrototypeOf。他的解释是,它并不是真正适用于
我正在阅读你不懂JS,并且混淆了 getPrototypeOf 和 isProtytypeOfs 的结果。代码如下: function Foo(name) { this.n
我正在尝试学习 javascript,并且正在从 Mozilla 文档中学习 Object。一切都很好,直到我看到这个例子。 It is the first example with the head
我有以下 Javascript 片段:` var alien = { kind: "alien" }; var person =
我正在阅读this post 。它定义了一个简单的数组: var myArray = [1, 2]; myArray.push(3); myArray.reverse(); myArray.pop()
非常简单: 我有代码使用Object.getPrototypeOf(...)来获取Dojo Widget的继承类(只是一个JS对象)。 IE8 不支持 Object.getPrototypeOf(..
Object.getPrototypeOf(obj) 是如何工作的? 根据定义,Object.getPrototypeOf(obj) 应该返回对象的原型(prototype)属性,或者以其他方式返回与
非常简单: 我有代码使用 Object.getPrototypeOf(...) 获取 Dojo Widget 的继承类(只是一个 JS 对象)。 IE8 不支持 Object.getPrototype
我正在学习一些 JS,我希望有人能用简单的术语向我解释 Object.getPrototypeOf() 与 .prototype 之间的区别 function ParentClass() {} fun
查看这个类的定义: function MyClass (property) { // Private var privateVariable; var privateMetho
所以我知道“null”是一种“对象” 但是,当我执行 Object.getProtoTypeOf(null) 时,我收到错误。 instanceof 也是如此,这有点相同。 如果 null 是一种 O
我很难理解 Object.getPrototypeOf(o)。我正在下面的代码中使用该方法。 var obj0 = {x: 10}; var obj1 = Object.create(null, {
我正在尝试设置一个对象的原型(prototype)。 但是,有时我想设置/获取字符串的原型(prototype)。然而,令人惊讶的是,当我调用时出现错误: var foo = 'baz'; Objec
我有一个 Angular 7 应用程序,当我在 IE11 中运行它时,我收到错误消息“Object.getPrototypeOf: 'this' is not an Object”。只有当我使用 --
Javascript 代理对象的一个应用是通过将数据作为数组的数组以及列出字段名称和每个字段索引的对象(即字段映射)通过网络发送来减少网络流量。 (而不是在每个对象中重复属性名称的对象数组)。 乍
我有这个原型(prototype)继承链: function Class1() {}; Class1.prototype.name = 'Main Class'; function Class2()
我知道后者是非标准的。但是 Object.getPrototypeOf 和 __proto__ 之间有区别吗?我正在研究 javascript 中的原型(prototype)链是如何工作的,并希望弄清
var protoRabbit = {size: "small"}; var fastRabbit = Object.create(protoRabbit); console.log(Object.g
我只是想知道替换 __proto__ 的主要原因是什么功能 getPrototypeOf/setPrototypeOf ? 提前致谢 最佳答案 MDN explains it pretty well.
我是一名优秀的程序员,十分优秀!