Link to home
Start Free TrialLog in
Avatar of HonorGod
HonorGodFlag for United States of America

asked on

What are closure and enclosure?

 I have encountered some references to "closure" and "enclosure" and am confused by these terms.
I'm looking for a clear, concise, and complete explanation of what closure and enclosure mean, especially with respect to JavaScript.

For example, search for "enclosure" on this page:

http://arstechnica.com/journals/linux.ars/2007/08/27/javascript-for-all-ages

and we find the following statement:

"To get a bit more privacy with your variables, you should use a closure by declaring an anonymous function and immediately calling it."

Unfortunately, it is not clear to me what is actually meant by this.  I'm having difficulty trying to wrap my head around these concepts.

The point value of this question is a reflection of my frustration with trying to find a clear, concise, and complete answer for this information.

Thanks in advance for your help and assistance.
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
PS: I think he meant closure and not enclosure
Avatar of HonorGod

ASKER

Part of my challenge is, as you (Michel) point out is the number of hits that google shows for that kind of search.  Hence the request for a clear, concise, and complete definition/explanation.

Michel, http://blog.morrisjohns.com/javascript_closures_for_dummies is an excellent reference (definition/explanation with examples) for closure.  Thank you!

I don't understand your PS.
I meant that in JS the word is closure and in the link you gave, he uses enclosure in the title.
It does Enclose the variables but I am not sure the word enclosure is used correctly here...
The "technical term" is closure. That article is using "enclosure" to stress the fact that values/references tend to get "trapped/enclosed" with functions. So, if you understand closures, then you are set.

A while back, someone posted some question that eventually led me to explained the concept of closures here:
https://www.experts-exchange.com/questions/22908876/Bookmarklet-How-to-link-to-js-code-on-a-given-web-server.html

You will have to read through it because the question was not specifically about closures. It just "took that route". Hopefully it is clear enough for you as it was for some of the other experts on that thread.
Michael, thanks for the time, and excellent pointers.