简体   繁体   English

为什么Object.getPrototypeOf()和Constructor.prototype记录不同的值

[英]Why Object.getPrototypeOf() and constructor.prototype logs different values

In following code, using getPrototypeOf() and constructor.prototype gives different values. 在以下代码中,使用getPrototypeOf()和Constructor.prototype给出不同的值。

function C(){ }
function D(){ }

C.prototype.fname = "John";  

console.log(Object.getPrototypeOf(C)); //function () {}
console.log(C.prototype); // C{fname: "John"}

That is because Object.prototype.isPrototypeOf() returns the prototype inherited from its parent constructor. 那是因为Object.prototype.isPrototypeOf()返回从其父构造函数继承的原型。

function C(){ }

Now this is constructed by primitive type Function 现在,这是由基本类型Function构造的

Hence when you call getPrototypeOf on C __Proto__ lookup from inherited parent constructor is returned(Read this description ). 因此,当您在C __Proto__上调用getPrototypeOf ,将返回继承的父构造函数的查找信息(请阅读此说明 )。

See the below for illustration: See the inline comments for understanding 请参阅以下插图:请参阅内联注释以了解

typeof C // "function"
C instanceof Function // True
Object.getPrototypeOf(C) === Function.constructor.prototype // "True"
Function.constructor.prototype // "function(){}"
Object.getPrototypeOf(C) // "function(){}"

Now the next part: 现在下一部分:

C.prototype.fname = "John";
typeof C.prototype // object

Hence its constructor becomes C() with prototype object chain inherited from C() which will be inherited when you create instance like this new C() 因此,它的构造成为C()与原型对象链从继承C()当你喜欢这个创建实例将继承new C()

Hence: 因此:

C.prototype.constructor // "function C(){}" -- Parent constructor

So when you lookup Prototype on C() it returns the prototype inherited from its contructor like below 因此,当您在C()上查找Prototype ,它将返回从其构造C()继承的prototype ,如下所示

C.prototype.constructor === C //true
C.prototype === C.prototype.constructor.prototype // True
C.prototype.constructor.prototype // C {fname: "John"}
C.prototype // C {fname: "John"}

Conclusion: 结论:

When ever you search for a prototype on a Object or Function lookup happens on the properties inherited from its parent constructor. 每当您在对象或函数上搜索原型时,都会在从其父构造函数继承的属性上进行查找。

Object.getPrototypeOf(C) === C.constructor.prototype //true
Object.getPrototypeOf(C) === Function.prototype // true
C.prototype === (new C()).__proto__ // true (this is for illustration only dont use __proto__ in your code)

Object.getPrototypeOf(C) is not equal to C.prototype because their constructors are different. Object.getPrototypeOf(C)不等于C.prototype因为它们的构造函数不同。


References: 参考文献:

console.log(Object.getPrototypeOf(C)); 

This returns the prototype of the class C rather than an instance of C. These should return the same result: 这将返回类C的原型,而不是C的实例。这些应返回相同的结果:

console.log(Object.getPrototypeOf(new C()));
console.log(C.prototype);

Object.getPrototypeOf(C); is basically doing the same thing that C.__proto__ does, which is it 基本上是在做C.__proto__一样的事情

"Points to the object which was used as prototype when the object was instantiated." “指向实例化对象时用作原型的对象。”

When C() was instantiated, it was a function. 实例化C()时,它是一个函数。 Check out this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto 检查一下: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto

And part of the description of Object.prototype from MDN: 以及来自MDN的Object.prototype描述的一部分:

"All objects in JavaScript are descended from Object; all objects inherit methods and properties from Object.prototype, although they may be overridden (except an Object with a null prototype, ie Object.create(null))." “ JavaScript中的所有对象都是从Object派生的;所有对象都继承自Object.prototype的方法和属性,尽管它们可能会被覆盖(具有空原型的Object,即Object.create(null)除外)。”

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype

Meaning C.prototype is just pointing to the object that contains the methods and properties an object instantiated from it will inherit (or in this case it was overridden to a string), while Object.getPrototypeOf(C) is looking at the initial object that was used to create C aka C.__proto__ . 意味着C.prototype只是指向包含从其实例化的对象将继承的方法和属性的对象(或在这种情况下,它被重写为字符串),而Object.getPrototypeOf(C)则在查看该对象的初始对象。用于创建C aka C.__proto__

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

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