Closures: why are they so useful?

前端 未结 10 679
渐次进展
渐次进展 2020-12-07 10:04

As an OO developer, maybe I have difficulty seeing its value. What added value do they give? Do they fit in an OO world?

10条回答
  •  难免孤独
    2020-12-07 10:45

    As to the concluding query: "Do (closures) fit in an OO world?"

    In many languages, closures, along with first-class functions, provide the very foundation for devising OO programs: Javascript, Lua and Perl come to mind.

    In another more "traditional" OO language with which I'm familiar, Java, there are 2 primary differences:

    1. Functions are not first class constructs
    2. In Java the implementation details of OO (whether closures are used internally) is not exposed directly to the developer as it is in Javascript, Lua and Perl

    Therefore in a "traditional OO" language such as Java, the addition of closures is largely just so much syntactic sugar.

提交回复
热议问题