本文介绍了[[Prototype]] vs原型:..有什么区别? (MyCons .__ proto__ === MyCons.prototype)等于FALSE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里似乎有区别......

It seems like there is a difference here...

假设我们有函数MyConstructor(){}

MyConstructor的 [[Prototype]] Function.prototype MyConstructor.prototype。

MyConstructor's [[Prototype]] is Function.prototype, not MyConstructor.prototype.

其他(非标准/console.log-able)字样:
$
MyConstructor .__ proto __ 不是 MyConstructor的 MyConstructor.prototype

In other (non-standard/"console.log-able") words:
MyConstructor.__ proto__ is not MyConstructor's MyConstructor.prototype

尝试这个:

function MyConstructor() {};
(MyConstructor.__proto__ === MyConstructor.prototype); //false?! why?

为什么会这样?有人可以向我解释差异吗?

Why is this so? Can someone explain it to me the difference?

推荐答案

想想这样吗。 MyConstructor 是一个函数对象,因此它是由 Function 创建的;因此它的 [[Prototype]] (或 __ proto __ )与 Function.prototype 。

Think of it like this. MyConstructor is a function object, so it was created by Function; therefore its [[Prototype]] (or __proto__) is identical to Function.prototype.

以同样的方式, var myObj = new MyConstructor()创建一个对象 myObj [[Prototype]] MyConstructor.prototype相同

In the same way, var myObj = new MyConstructor() creates an object myObj with a [[Prototype]] identical to MyConstructor.prototype.

换句话说,函数有一个 prototype 属性,当你用 new ,他们将构造一个对象,其 [[Prototype]] 与构造函数的相同原型属性...但是函数的原型属性与它的 [[Prototype]]不是一样的(或 __ proto __ )属性,因为函数遵循与其他对象相同的规则并获取其内部 [[Prototype]] 来自构造它的函数的属性(它总是函数,顺便说一句。)

To put it another way, functions have a prototype property, and when you invoke functions with new, they will construct an object having a [[Prototype]] identical to the constructor function's prototype property... however a function's prototype property is not the same thing as its [[Prototype]] (or __proto__) property, because a function follows the same rules as other objects and gets its internal [[Prototype]] property from the function that constructed it (which is always Function, incidentally).

为了进一步解释, [[Prototype]] prototype 有完全不同的用途。在解析对象的属性时使用 [[Prototype]] 。如果一个对象没有属性,则检查其 [[Prototype]] ,然后检查该对象的 [[Prototype]] ,依此类推,直到找到一个属性或者你到达原型链的末尾。

To explain further, [[Prototype]] and prototype have entirely different purposes. [[Prototype]] is used when resolving an object's properties. If an object doesn't have a property, its [[Prototype]] is checked, and then that object's [[Prototype]], and so on, until either a property is found or you hit the end of the prototype chain.

相比之下,原型是将 [[Prototype]] 属性分配给对象的机制,因为除了非对象之外,您无法直接访问它们标准 __ proto __ 属性。

In contrast, prototype is the mechanism by which you assign [[Prototype]] properties to objects, since you can't access them directly other than with the non-standard __proto__ property.

由于函数是对象,它们都有一个 [[Prototype]] 内部属性,用于解析属性使用普通对象和 prototype 属性,该属性被指定为由 [[Prototype]] 构造的新对象功能。

Since functions are objects, they have both a [[Prototype]] internal property, used to resolve properties as with normal objects, and a prototype property, which is assigned as the [[Prototype]] of new objects constructed by the function.

这篇关于[[Prototype]] vs原型:..有什么区别? (MyCons .__ proto__ === MyCons.prototype)等于FALSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 04:44