当其构造函数的原型不是对象时,如何创建对象? [英] How objects are created when the prototype of their constructor isn't an object?
问题描述
我回答了这个问题:如何创建对象?
,但回答完之后我还有一个问题。
I answered this question: How do objects created? but I also have a question after I answered it.
function Bar() {
this.foo = true;
}
console.log('obj3');
Bar.prototype = 3;
var obj3 = new Bar();
console.log(obj3);
console.log(obj3.constructor === Bar);
console.log(obj3.constructor === Object);
console.log(Object.prototype === Object.getPrototypeOf(obj3));
console.log(obj3.foo);
Bar.prototype = 3
,我的理论是,当 new Bar()
执行,如步骤2 所示,应该将创建的对象链接到 Bar.prototype
,但是由于 Bar.prototype
不引用对象,暗含了默认值,即 Object.prototype
。
Bar.prototype = 3
, my theory is that, when new Bar()
executed, as in Step 2, the created object is supposed to linked to Bar.prototype
, but since the value of Bar.prototype
doesn't refer to an object, implicitly a default value was assigned, which is the Object.prototype
.
由于 object.prototype.constructor
对 Object $的引用c $ c>,
obj3.constructor
也引用 Object
,但 obj3 由于第1步,code>的事实构造函数仍为
Bar
由 console.log(obj3.foo)证明; //正确
。
Due to object.prototype.constructor
's reference to the Object
, obj3.constructor
also refer to the Object
, but obj3
's de facto constructor is still Bar
, because of Step 1, which also can be proved by console.log(obj3.foo); // true
.
是吗?
@Leo问我是否可以提供有关内部机制的更多信息。他已经在Firefox Chrome Safari中对其进行了测试,它们的行为都相同,他认为这应该在ECMA-262中明确规定。但是,他来对地方了。但是我也没有找到任何证据可以支持我的论点,因此,我正在寻求您的帮助。您能否提供有关内部机制的更多信息?
@Leo asked me if I could provide more information about the internal mechanism. He had tested it in Firefox Chrome Safari, they all behave the same, he thinks it should be something clearly specified in ECMA-262. However, he hasn't got to the right place. But I didn't find anything to support my argument too, therefore I'm looking for your help. Could you provide more information about the internal mechanism?
推荐答案
是的,这是在 GetPrototypeFromConstructor :
- Let proto be ? Get(constructor,
"prototype"
). If Type(proto) is not Object, then
- 让境界为? GetFunctionRealm ( constructor )
- 让 proto 为领域的内部对象,名为 intrinsicDefaultProto 。
- Let realm be ? GetFunctionRealm(constructor).
- Let proto be realm's intrinsic object named intrinsicDefaultProto.
>
具体来说,它是这样的:
Specifically, it works like this:
-
new Bar()
调用Bar。 [[Construct]]
- 初始化 thisArgument 使用
OrdinaryCreateFromConstructor ( newTarget ,%ObjectPrototype%
) - 转动从
继承的对象 GetPrototypeFromConstructor (构造函数, intrinsicDefaultProto )
new Bar()
callsBar.[[Construct]]
- That initializes the thisArgument using
OrdinaryCreateFromConstructor(newTarget,"%ObjectPrototype%"
) - That returns an object which inherits from
GetPrototypeFromConstructor(constructor, intrinsicDefaultProto)
因此,如果 prototype
不是对象,默认值为该领域的%ObjectPrototype%。
So if the prototype
is not an object, the default will be the %ObjectPrototype% of that realm.
请注意,并非总是 Object.prototype
:
var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
var win = iframe.contentWindow;
document.body.removeChild(iframe);
var F = win.Function("")
F.prototype = 5;
var proto = Object.getPrototypeOf(new F());
proto === Object.prototype; // false
proto === win.Object.prototype; // true
这篇关于当其构造函数的原型不是对象时,如何创建对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!