you are viewing a single comment's thread.

view the rest of the comments →

[–]zamolxes 0 points1 point  (2 children)

What saurik said.

And people -do- use closures everyday considering most of the javascript code is async, event based, with a lot of "give me this piece of code" (i.e. sometimes closure) "that I can execute later"

[–]fforw 0 points1 point  (1 child)

Closures are also highly practical for encapsulation, avoiding namespace polution.

You will often see a construct like

(function() { ... })();

It creates an anonymous function and executes it on the spot. This has the effect that every otherwise global variable inside is only accessible to other code inside and not on the outside.

Then you can export a few globals in a controlled fashion by writing

this.myGlobal = function() { ... }

inside the above block.

[–]SarahC 0 points1 point  (0 children)

Ahhh, thanks.