本文介绍了JavaScript中[[Prototype]]与原型的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自:

两者之间的关系有哪些细节?我通过ECMA 262浏览了所有我读过的内容如下:

What are the details of the relationship between the two? I've browsed through ECMA 262 and all I've read there is stuff like:

本机ECMAScript对象有一个名为[[Prototype]]的内部属性。该属性的值为null或对象,用于实现继承。

Native ECMAScript objects have an internal property called [[Prototype]]. The value of this property is either null or an object and is used for implementing inheritance.

每个内置函数和每个内置构造函数都有Function原型对象,这是表达式Function.prototype的初始值

Every built-in function and every built-in constructor has the Function prototype object, which is the initial value of the expression Function.prototype

每个内置原型对象都有Object原型对象,它是表达式
的初始值Object.prototype(15.3.2.1),作为其内部[[Prototype]]属性的值,除了Object
原型对象本身。

Every built-in prototype object has the Object prototype object, which is the initial value of the expression Object.prototype (15.3.2.1), as the value of its internal [[Prototype]] property, except the Object prototype object itself.

从这一切我收集到的是[[Prototype]]属性相当于几乎任何对象的 prototype 属性。我错了吗?

From this all I gather is that the [[Prototype]] property is equivalent to the prototype property for pretty much any object. Am I mistaken?

推荐答案

我相信你在大多数情况下是对的。

I believe you are right in most cases.

每个对象都有一个隐藏的 [[Prototype]] 属性,用于继承。函数还有一个公共 prototype 属性,仅当函数用作构造函数时才使用:当使用 new ,新对象的 [[Prototype]] 属性设置为函数的 prototype 属性用作构造函数。

Every object has a hidden [[Prototype]] property, which is used for inheritance. Functions additionally have a public prototype property, which is used only when the function is used as constructor: When an object is constructed using new, the [[Prototype]] property of the new object is set to the prototype property of the function that was used as constructor.

例如

function C() {}
C.prototype = P1;  
var obj = new C();  // obj.[[Prototype]] is now P1.

你可以得到 [[Prototype]] 使用 Object.getPrototypeOf(< obj>)的属性。 (此方法在ECMAScript 5中指定。较旧版本的JavaScript没有任何标准方法来读取 [[Prototype]] )。

You can get the [[Prototype]] property using Object.getPrototypeOf(<obj>). (This method is specified in ECMAScript 5. Older versions of JavaScript does not have any standard way of reading [[Prototype]]).

你可以通常通过构造函数获取原型,例如:

You can usually get to the prototype through the constructor, e.g.:

obj.constructor.prototype == Object.getPrototypeOf(obj) 

但情况并非如此,因为可以重新分配构造函数的prototype属性,但是在创建对象后无法重新分配对象的 [[Prototype]] 。所以如果你这样做:

But this is not always the case, since the prototype property of the constructor function can be reassigned, but the [[Prototype]] of an object cannot be reassigned after the object is created. So if you do:

C.prototype = P2;

然后

obj.constructor.prototype != Object.getPrototypeOf(obj)

因为原型 C 现在 P2 ,但 [[原型]] obj 仍为 P1

Because the prototype of C is now P2, but [[Prototype]] of obj is still P1.

请注意是具有原型属性的函数。另请注意,函数的 prototype 属性与函数的 [[Prototype]] 属性不同!

Note that it is only functions that have a prototype property. Note also that the prototype property of a function is not the same as the [[Prototype]] property of the function!

这篇关于JavaScript中[[Prototype]]与原型的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 20:44