我正在查看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 Temp
和new Temp
Temp.prototype = prototype;
return new Temp;
答案 0 :(得分:1)
可能只是因为它不会缓存最后一个对象并在原始对象被删除时将其保留在内存中。在绝大多数情况下似乎没有必要,但谨慎的做法并不是一个坏主意。