1.循环引用:
function SetupLeak() // 产生循环引用,因此会造成内存泄露
{
// First set up the script scope to element reference
myGlobalObject =
document.getElementById(
" LeakedDiv " );
// Next set up the element to script scope reference
document.getElementById( " LeakedDiv " ).expandoProperty =
myGlobalObject;
}
2.闭包:
function outer(){
var obj = {};
function inner(){
//这里引用了obj对象
}
obj.inner = inner;
}
本文通过两个具体的示例详细解析了JavaScript中闭包和循环引用的概念及其如何造成内存泄漏的问题。首先介绍了如何通过闭包使得函数内的局部变量在外部被访问,其次展示了循环引用的形成过程,并解释了其为何会导致内存无法释放。
13万+

被折叠的 条评论
为什么被折叠?



