What is a 'Closure'?

前端 未结 23 1191
星月不相逢
星月不相逢 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

    If you are from the Java world, you can compare a closure with a member function of a class. Look at this example

    var f=function(){
      var a=7;
      var g=function(){
        return a;
      }
      return g;
    }
    

    The function g is a closure: g closes a in. So g can be compared with a member function, a can be compared with a class field, and the function f with a class.

提交回复
热议问题