我正在尝试确保一些包含在闭包中的变量在使用完后能够被垃圾回收。我不确定将它们设置为未定义或删除它们是否足够。你有什么想法吗?
// run once for each photo, could be hundreds
$("img.photo").each( function(){
// create the vars to put in the callback
var photo = $(this);
var tmp = new Image();
// set the callback, wrapping the vars in its scope
tmp.onload = (function(p,t){
return function(){
// do some stuff
// mark the vars for garbage collection
t.onload = ?
t = ?
p = ?
})(photo, tmp)
// set the source, which calls onload when loaded
tmp.src = photo.attr("src")
})
delete
不会释放内存。它只是删除一个引用(这很好,否则var x = {a: 0}; var y = x; delete x; alert(y.a)
将会造成严重后果)。而且你不能删除变量。 - user395760