Object.create源解释?

时间:2016-03-05 15:53:25

标签: javascript

我正在查看MSDN网络,任何人都可以向我解释source-code吗?

Object.create = (function() {
    var Temp = function() {};
    return function (prototype) {
      if (arguments.length > 1) {
        throw Error('Second argument not supported');
      }
      if (typeof prototype != 'object') {
        throw TypeError('Argument must be an object');
      }
      Temp.prototype = prototype;
      var result = new Temp();
      Temp.prototype = null;
      return result;
    };
  })();

function Guru(name){
   this.name = name;
}


function Shankar(name){
   this.name = name;
}

Guru.prototype = Object.create(Shankar.prototype);

令我感到困惑的是Temp.prototype = null;,为什么我们只需null

就可以将其设置为returning a instance of Tempnew Temp
Temp.prototype = prototype;
return new Temp;

1 个答案:

答案 0 :(得分:1)

可能只是因为它不会缓存最后一个对象并在原始对象被删除时将其保留在内存中。在绝大多数情况下似乎没有必要,但谨慎的做法并不是一个坏主意。

相关问题