4.11 CLOSURE SCENARIOS

4.11.1 Simplify Scope

Other common Object-Oriented languages like C# have a concept of block scope. JavaScript does not. The scopes it can handle are either global or local within a function. This perceived simplicity on the other hand makes it clumsy to handle since variables are littered all over the place. Closures act as a means to simplify.

4.11.2 Return Methods from Functions

The idea is to encapsulate private logic and expose only certain, “public” methods. Take a variation of the above methods to implement a counter but with a difference—to reuse the same counter for both the increments. Also implement it by making a property of the returned function (in this case it is incrFuncs).

Take care to click on Initialize ...

Get Web Technology: Theory and Practice now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.