6

MDN のObject.createpolyfillを考慮すると:

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();
    };
  })();
}

特に次の 2 行に注目してください。

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

私は疑問に思っていましたが、なぜ設定するのが適切ではないのF.prototype.constructor = F;ですか?

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

1 に答える 1

3

F.prototype.constructor = F; を設定するのが適切ではないのはなぜですか?

F一時的な関数であり、外部から参照する方法がないように意図的に思われObject.createます。

于 2015-06-17T20:11:37.083 に答える