繁体   English   中英

为什么MDN的`Object.create` polyfill没有设置`prototype.constructor`?

[英]Why doesn't MDN's `Object.create` polyfill set `prototype.constructor`?

考虑MDN的Object.create polyfill

if (typeof Object.create != 'function') {
  (function () {
    var F = function () {};
    Object.create = function (o) {
      if (arguments.length > 1) { throw Error('Second argument not supported');}
      if (o === null) { throw Error('Cannot set a null [[Prototype]]');}
      if (typeof o != 'object') { throw TypeError('Argument must be an object');}
      F.prototype = o;
      return new F();
    };
  })();
}

特别关注这两条线:

F.prototype = o;
return new F();

我在想,为什么设置F.prototype.constructor = F;

F.prototype = o;
F.prototype.constructor = F;  // why not?
return new F();

我在想,为什么不适合设置F.prototype.constructor = F;?

F是一个临时函数,似乎有意无法从外部Object.create引用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM