我正在查看 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;
最佳答案
可能只是为了在原始对象被删除时它不会缓存最后一个对象并将其保留在内存中。在绝大多数情况下似乎没有必要,但出于谨慎考虑,这并不是一个坏主意。
关于javascript - Object.create源码解释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35816222/