Skip to content
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

闭包 #1

Open
liuduanyang opened this issue Dec 3, 2017 · 0 comments
Open

闭包 #1

liuduanyang opened this issue Dec 3, 2017 · 0 comments

Comments

@liuduanyang
Copy link
Owner

liuduanyang commented Dec 3, 2017

闭包:函数在定义时的词法作用域以外的地方被调用,该函数仍然可以继续访问定义时的词法作用域。

闭包举例:

function wai(){
var a=10;
function nei(){
	console.log(a);
}
   return nei;
 }
var a=11;
var fun=wai();
fun();    //10

因为fun引用了wai函数的执行结果(即wai函数的返回值nei函数)所以nei函数的作用域被保留,当fun函数执行时,按照正常的查询作用域即可。

无论使用何种方式对函数类型的值进行传递,当函数在别处被调用时都可以观察到闭包。

总结:无论通过何种手段将内部函数传递到所在的词法作用域以外,它都会持有对原始定义作用域的引用,无论在何处执行这个函数都会使用闭包。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant