-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
第165题:闭包的使用场景,使用闭包需要注意什么 #453
Comments
闭包什么是闭包闭包很简单,就是能够访问另一个函数作用域变量的函数,更简单的说,闭包就是函数,只不过是声明在其它函数内部而已。 例如: function getOuter(){
var count = 0
function getCount(num){
count += num
console.log(count) //访问外部的date
}
return getCount //外部函数返回
}
var myfunc = getOuter()
myfunc(1) // 1
myfunc(2) // 3
当 function add(x){
return function(y){
return x + y
};
}
var addFun1 = add(4)
var addFun2 = add(9)
console.log(addFun1(2)) //6
console.log(addFun2(2)) //11
即:
所以,闭包可以:
使用闭包应该注意什么
应用场景闭包通常用来创建内部变量,使得这些变量不能被外部随意修改,同时又可以通过指定的函数接口来操作。例如
|
No description provided.
The text was updated successfully, but these errors were encountered: