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

闭包 #15

Open
dashengzi66 opened this issue Aug 20, 2021 · 1 comment
Open

闭包 #15

dashengzi66 opened this issue Aug 20, 2021 · 1 comment

Comments

@dashengzi66
Copy link
Owner

dashengzi66 commented Aug 20, 2021

定义

红宝书:闭包是指有权访问另外一个函数作用域中的变量的函数

MDN:闭包是指那些能够访问自由变量的函数--现在MDN已经修改闭包的定义

  • 其中自由变量,指在函数中使用的,但既不是函数参数arguments也不是函数的局部变量的变量,其实就是另外一个函数作用域中的变量。
  • 闭包让你可以在一个内层函数中访问到其外层函数的作用域
@dashengzi66
Copy link
Owner Author

自由变量

在A作用域中使用的变量x,却没有在A作用域中声明(即在其他作用域中声明的),对于A作用域来说,x就是一个自由变量

let x = 6;
function fn(){
  let b=10;
  console.log(x+b); //x在这里就是一个自由变量
}

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