我想使用以下代码来存储匿名类实例; cleanup()
函数是确保在内存中没有任何东西悬空的正确方法吗?
var instances = [];
// example Class
function Ugh() {}
Ugh.prototype.stop = function() {
// do stuff
};
// push some anonymous instances
instances.push(new Ugh());
instances.push(new Ugh());
instances.push(new Ugh());
// code I'm unsure of, is is correct?
function cleanup() {
instances.forEach(function(item, i, arr) {
item.stop();
arr[i] = null; // nullify the 'pointer'
});
instances.length = 0; // is this needed?
}
cleanup();
提前致谢。