考虑到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();