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

闭包-定义用例错误 #39

Open
Pengjee opened this issue Dec 31, 2023 · 0 comments
Open

闭包-定义用例错误 #39

Pengjee opened this issue Dec 31, 2023 · 0 comments

Comments

@Pengjee
Copy link

Pengjee commented Dec 31, 2023

function foo() {
  let value = 1;

  function bar() {
    console.log(value);
  }
  // error,正确的应该是return bar
  return bar();
}

const baz = foo();

// 这就是闭包的作用,调用 foo 函数,就会执行里面的 bar 函数,foo 函数这时就会访问函数外层的变量
baz();
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