请问这种情况是闭包吗?

作者站长头像
站长
· 阅读数 15

    const Fns = []

    function foo() {
      Fns.forEach( fn => {
        fn()
      })
    }

    function bar() {
      function test() {
        return 'test'
      }

      Fns.push(() => {
        const value = test()
        print(value)
      })

      function print(value) {
        console.log(value)
      }

      foo()

    }

    bar()

请问在我push进这两个的时候,又在foo函数中调用,这个是否构成闭包?

回复
1个回答
avatar
test
2024-07-17

当打印出 test的时候就是闭包了,const value = test()

回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容