How is a closure different from a callback?

前端 未结 9 1079
伪装坚强ぢ
伪装坚强ぢ 2020-12-07 19:08

I asked a question about callbacks and arrived at another question (see comment). How is a closure different from a callback?

9条回答
  •  盖世英雄少女心
    2020-12-07 19:53

    There's a good definition of closures here:

    A "closure" is an expression (typically a function) that can have free variables together with an environment that binds those variables (that "closes" the expression).

    In practice, that means it's a function that has some hidden variables.

    A callback is a higher-level idea. Generally it is a function which is passed around with the intent of being called at a later time. In JavaScript, closures are often used as callbacks.

提交回复
热议问题