+ 5
Please illustrate JavaScript closure with example.
I have been reading JavaScript closure all day, through online tutorials ,books, but my basic concept is not clear.
3 Antworten
+ 9
Hi @Renu... Excellent question!!! I'll try to explain this in a way that I would have wanted someone to explain it. ;)
On the surface, a closure is a function nested within another function.
What's not so obvious is how closures can assist with defining and managing variables and functions meant to have a private scope.
One thing to keep in mind is that closures have access to the global scope AND to the scope of their parent function.
To see this in action, run the code snippet below, which I put together just to answer this question. Also, review the detailed comments to follow my thought process.
- https://code.sololearn.com/W41q8fBJ4tO4/#js
Hopefully, this will all make sense and help with your understanding of closures.
[Update]
Also... here is another good article explaining closures:
- http://javascriptissexy.com/understand-javascript-closures-with-ease/
+ 4
@Renu Are there any specific questions about closures you still have that I can help you better understand?
+ 3
Thanks David, for explaining us about closures in details.