JavaScript 函数的prototype 属性的初始值是多少? [英] What is the initial value of a JavaScript function's prototype property?
问题描述
我知道 JavaScript 函数对象的 prototype
属性被复制到内部的 [[Prototype]]
属性(又名 __proto__
)使用函数作为构造函数实例化的对象.因此,我可以将此属性设置为我想充当原型的任何对象:
I know that the prototype
property of JavaScript function objects is copied to the internal [[Prototype]]
property (a.k.a. __proto__
) of objects instantiated by using the function as a constructor. So, I can set this property to any object that I want to act as the prototype:
function Foo() {}
Foo.prototype = {
toString: function() { return "I'm a Foo!"; }
}
new Foo().toString()
// --> "I'm a Foo!"
向新生成的函数的现有原型添加应该作为类方法的函数似乎是一种普遍的做法:
It seems that it is widespread practice to add functions that should act as class methods to the existing prototype of newly generated functions like this:
function Bar() {}
Bar.prototype.toString = function() {
return "I'm a Bar!";
}
new Bar().toString()
// --> "I'm a Bar!"
不过,我不清楚 prototype
属性的初始值是什么.
It is unclear to me, though, what the initial value of the prototype
property is.
function Baz() {}
Baz.prototype
// --> Baz {
// constructor: function Baz() {},
// __proto__: Object
// }
注释显示了 Chrome 的 JavaScript 控制台打印的内容.这是否意味着我创建的每个函数实际上都创建了两个对象?一种用于函数本身(构造函数
),一种用于其原型?
The comment shows what Chrome's JavaScript console prints. Does this mean that every function that I create actually creates two objects? One for the function itself (the constructor
) and one for its prototype?
这是在 ECMAScript 标准中的某个地方定义的吗?我试图找到它,但找不到.所有浏览器的处理方式都一样吗?
Is this defined somewhere in the ECMAScript standard? I tried to find it but couldn't. Do all browsers handle this in the same way?
推荐答案
prototype
的初始值在任何新创建的 Function
实例上都是 的一个新实例>Object
,但将自己的属性 constructor
设置为指向新函数.
The initial value of prototype
on any newly-created Function
instance is a new instance of Object
, but with the own-property constructor
set to point back to the new function.
这在 ECMA262-5 的第 13.2 节中以典型的 ECMAScript 规范完全不可读的方式描述:
This is described in typical ECMAScript-spec completely-unreadable fashion by ECMA262-5 in section 13.2:
(16.) 让 proto 成为创建一个新对象的结果,该对象将由表达式 new Object()
构造,其中 Object
> 是具有该名称的标准内置构造函数
(16.) Let proto be the result of creating a new object as would be constructed by the expression
new Object()
whereObject
is the standard built-in constructor with that name
(17.) 调用 proto 的 [[DefineOwnProperty]] 内部方法,参数为constructor",属性描述符{[[Value]]: F, { [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true}, and false.
(17.) Call the [[DefineOwnProperty]] internal method of proto with arguments "constructor", Property Descriptor {[[Value]]: F, { [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true}, and false.
(18.) 调用 F 的 [[DefineOwnProperty]] 内部方法,参数为prototype",属性描述符 {[[Value]]: proto
, { [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false}, and false.
(18.) Call the [[DefineOwnProperty]] internal method of F with arguments "prototype", Property Descriptor {[[Value]]: proto
, { [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false}, and false.
这篇关于JavaScript 函数的prototype 属性的初始值是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!