Skip to content

闭包-定义用例错误 #39

Description

@Pengjee
function foo() {
  let value = 1;

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

const baz = foo();

// 这就是闭包的作用,调用 foo 函数,就会执行里面的 bar 函数,foo 函数这时就会访问函数外层的变量
baz();

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions