javascript - Object.create源码解释?

标签 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; ,为什么我们将其设置为 nullreturning a instance of Temp当我们可以返回new Temp

Temp.prototype = prototype;
return new Temp;

最佳答案

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

关于javascript - Object.create源码解释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35816222/

相关文章:

javascript - 使用动态可观察对象进行 KnockoutJS 验证

javascript - 聚合 Mithril 中的成分

javascript - 减少在同一 HTML 元素上使用 getElementById 的次数

javascript - 通过单击或箭头键选择下拉选择时的行为不同

javascript - 解释一下 "document.getElementById("div_name").innerHTML=xmlHttp.responseText;"

javascript - grunt sass 和 js 自动加载器

javascript - jQuery uidatepicker 按钮图像不起作用

javascript - 使用 OrgChart 增加节点宽度的最佳方法

javascript - 最小形式 BACK 按钮 questionNumber interference

javascript - 为什么 SVG 不会在 chrome 扩展中加载?