O'Reilly logo

Web Technology: Theory and Practice by M. Srinivasan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required