简体   繁体   English

Object.prototype的构造函数

[英]The constructor of Object.prototype

In JavaScript, every object inherits its properties and methods from a specific prototype, where prototypes are objects. 在JavaScript中,每个对象都从特定的原型(其中原型是对象)继承其属性和方法。

The inheritance forms a prototype chain where (Object.prototype) stands at its top (followed by null which has no properties or methods) and all the objects inherit from it (unless someone else inserts other changes to the prototype chain). 继承形成一个原型链,其中(Object.prototype)位于其顶部(其后是没有属性或方法的null),并且所有对象都从该继承链继承(除非其他人将其他更改插入到原型链中)。

If (Object.prototype) is an object, what is its constructor? 如果(Object.prototype)是一个对象,它的构造函数是什么?

I mean what completes this expression in order to be evaluated to true. 我的意思是什么使此表达式完整,以便被评估为真。

Object.prototype instanceof .....

From "this and Object Prototypes" book of "You don't know JS" series by Kyle Simpsion 摘自Kyle Simpsion 撰写的“您不了解JS”系列的“ this和Object Prototypes”

function Foo() {
    // ...
}

Foo.prototype.constructor === Foo; // true

var a = new Foo();
a.constructor === Foo; // true

The Foo.prototype object by default (at declaration time on line 1 of the snippet!) gets a public, non-enumerable (see Chapter 3) property called .constructor , and this property is a reference back to the function (Foo in this case) that the object is associated with. Foo.prototype对象默认情况下(在代码段的第1行声明时!)获得一个名为.constructor的公共,不可枚举的属性(请参见第3章),并且该属性是对函数的引用(本.constructor Foo)。情况)与对象相关联。 Moreover, we see that object a created by the "constructor" call new Foo() seems to also have a property on it called .constructor which similarly points to "the function which created it". 此外,我们看到由“构造函数”调用new Foo()创建的对象a似乎也具有名为.constructor的属性,该属性类似地指向“创建它的函数”。

Note: This is not actually true. 注意:实际上并非如此。 a has no .constructor property on it, and though a.constructor does in fact resolve to the Foo function, "constructor" does not actually mean "was constructed by", as it appears. a上没有.constructor属性,尽管a.constructor实际上可以解析为Foo函数,但“ constructor”实际上并不表示“是由...构造的”。 We'll explain this strangeness shortly. 我们将在短期内解释这种奇怪之处。

... ...

"Objects in JavaScript have an internal property, denoted in the specification as [[Prototype]], which is simply a reference to another object.". “ JavaScript中的对象具有内部属性,在规范中用[[Prototype]]表示,它只是对另一个对象的引用。”

So, Object.prototype itself is not an object. 因此,Object.prototype本身不是对象。 As to your specific question about instanceof: 关于您关于instanceof的特定问题:

var a = new Function();
a.prototype instanceof Object; //true
var b = new String();
b.prototype instanceof Object; //false

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

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