千锋教育JavaScript全套视频教程(10天学会Js,前端javascrip

闭包:
函数内部返回一个函数,这个函数被外界引用,那么这个函数就不会被浏览器的回收机制销毁回收,内部函数所用到的外部函数的变量也不会被销毁。
优点:让临时变量永驻内存
缺点:内存泄露
案例1:
function outer(){
var name = 'kerwin'
var age = 100
return function(){
return name + '111111'
}
}
var func = outer();
console.log(func());
func()
func()
案例2:
fetch('http://www.a.com/aaa')
fetch('http://www.a.com/bbb')
fetch('http://www.b.com/aaa')
fetch('http://www.b.com/bbb')
function FetchContainer(url){
return function(path){
return fetch(url+path)
}
}
var fetcha=FetchContainer('http://www.a.com')
fetcha('/aaa').then(res=>res.json()).then(res=>console.log(res))
fetcha('/bbb').then(res=>res.json()).then(res=>console.log(res))
fetcha('/ccc').then(res=>res.json()).then(res=>console.log(res))
fetcha('/ddd').then(res=>res.json()).then(res=>console.log(res))
fetcha = null; //解决内存泄露问题把使用完的变量设置为null。