当前位置:网站首页>JS closure

JS closure

2022-07-04 20:44:00 Big chicken legs are best

Simply speaking , That is, the internal function can read the variables of the external function , And these variables will be saved in memory , It will not be automatically cleared after the outer function is called

advantage

Variables are stored in memory for a long time , Avoid the pollution of global variables , Mimic block level scope

for(var i=0;i<5;i++){
        (function f(i){
            let t=i
            setTimeout(()=>{
                console.log(i)
            })
        })(i)
    
    }

shortcoming

memory-resident , Will increase memory usage , Improper use will cause memory leakage

原网站

版权声明
本文为[Big chicken legs are best]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041903416243.html