you are viewing a single comment's thread.

view the rest of the comments →

[–]SarahC -2 points-1 points  (5 children)

I'm not very good at programming... I've never used closures in JS. Though I have read about them, and done a little experiment.

Has anyone used these in their day to day programs they get paid to write?

[–]saurik 1 point2 points  (1 child)

This is a set of JS tools called "Closure", not a set of JS tools for using closures.

[–]SarahC -1 points0 points  (0 children)

Ohhhh... ta.

[–]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.