![](/img/trans.png)
[英]Why is the Temp.prototype on the MDN Object.create polyfill set to null?
[英]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.