问题
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, 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.
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.
Am I right?
@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?
回答1:
Yes, this is specified in GetPrototypeFromConstructor:
- Let proto be ? Get(constructor,
"prototype").If Type(proto) is not Object, then
- Let realm be ? GetFunctionRealm(constructor).
- Let proto be realm's intrinsic object named intrinsicDefaultProto.
Specifically, it works like this:
new Bar()callsBar.[[Construct]]- That initializes the thisArgument using
OrdinaryCreateFromConstructor(newTarget,"%ObjectPrototype%") - That returns an object which inherits from
GetPrototypeFromConstructor(constructor, intrinsicDefaultProto)
So if the prototypeis not an object, the default will be the %ObjectPrototype% of that realm.
Note that it's not always 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
来源:https://stackoverflow.com/questions/41846565/how-objects-are-created-when-the-prototype-of-their-constructor-isnt-an-object