What is a 'Closure'?

前端 未结 23 1234
星月不相逢
星月不相逢 2020-11-22 08:02

I asked a question about Currying and closures were mentioned. What is a closure? How does it relate to currying?

23条回答
  •  逝去的感伤
    2020-11-22 08:55

    Functions containing no free variables are called pure functions.

    Functions containing one or more free variables are called closures.

    var pure = function pure(x){
      return x 
      // only own environment is used
    }
    
    var foo = "bar"
    
    var closure = function closure(){
      return foo 
      // foo is a free variable from the outer environment
    }
    

    src: https://leanpub.com/javascriptallongesix/read#leanpub-auto-if-functions-without-free-variables-are-pure-are-closures-impure

提交回复
热议问题