为什么MDN Object.create polyfill上的Temp.prototype设置为null? [英] Why is the Temp.prototype on the MDN Object.create polyfill set to null?
本文介绍了为什么MDN Object.create polyfill上的Temp.prototype设置为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么Object.create的MDN polyfill具有以下行:
Why does the MDN polyfill for Object.create have the following line:
Temp.prototype = null;
我们是否避免维护对原型参数的引用,从而加快垃圾收集速度?
Is it so that we avoid maintaining a reference to the prototype argument enabling faster garbage collection?
polyfill:
if (typeof Object.create != 'function') {
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;
};
})();
}
推荐答案
是的,完全正确。这个polyfill确实将 Temp
函数永久保存在内存中(因此它的平均速度更快,不需要为每次调用创建一个函数
),并在其上重置 .prototype
是必要的,这样它就不会泄漏。
Yes, exactly. This polyfill does hold the Temp
function forever in memory (so that its faster on average, not needing to create a function for every invocation of create
), and resetting the .prototype
on it is necessary so that it does not leak.
这篇关于为什么MDN Object.create polyfill上的Temp.prototype设置为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文