当其构造函数的原型不是对象时,如何创建对象? [英] How objects are created when the prototype of their constructor isn't an object?

查看:100
本文介绍了当其构造函数的原型不是对象时,如何创建对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我回答了这个问题:如何创建对象?
,但回答完之后我还有一个问题。

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 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



  1. proto 是? 获取构造函数原型 )。

  2. 如果类型 proto )不是对象,那么

  1. Let proto be ? Get(constructor, "prototype").
  2. If Type(proto) is not Object, then


  1. 境界为? GetFunctionRealm constructor

  2. proto 为领域的内部对象,名为 intrinsicDefaultProto

  1. Let realm be ? GetFunctionRealm(constructor).
  2. Let proto be realm's intrinsic object named intrinsicDefaultProto.

>


具体来说,它是这样的:

Specifically, it works like this:


  1. new Bar()调用 Bar。 [[Construct]]

  2. 初始化 thisArgument 使用

    OrdinaryCreateFromConstructor newTarget %ObjectPrototype%

  3. 转动从

    继承的对象 GetPrototypeFromConstructor 构造函数 intrinsicDefaultProto

  1. new Bar() calls Bar.[[Construct]]
  2. That initializes the thisArgument using
    OrdinaryCreateFromConstructor(newTarget, "%ObjectPrototype%")
  3. That returns an object which inherits from
    GetPrototypeFromConstructor(constructor, intrinsicDefaultProto)

因此,如果 prototype 不是对象,默认值为该领域的%ObjectPrototype%。

So if the prototypeis 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆