What is so special about closures?

前端 未结 2 1993
时光说笑
时光说笑 2021-02-02 01:12

I\'ve been reading this article about closures in which they say:

  • \"all the plumbing is automatic\"
  • the compiler \"creates a wrapper class\" and \"extends
2条回答
  •  醉话见心
    2021-02-02 01:46

    The closure is a functionality of the Compiler. You don't see it, it just makes the code you write work.

    Without it, the call to AddToIt(3) will fail, because the underlying lamda uses the local variable a = 27 in the scope of AddToItClusure(). This variable is does not exist when AddToIt is called.

    But because of the Closure, a mechanism used by the compiler, the code works and you don't have to care about it.

提交回复
热议问题