A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function's scope from an inner function.
A Closure is a combination of a function enclosed with references to its surrounding state (the lexical environment). In JavaScript, closures are created every time a function is created at run time. In other words, a closure is just a fancy name for a function that remembers the external things used inside it.
If JavaScript did not have closures, then more states would have to be passed between functions explicitly, making parameter lists longer and code noisier. So, if you want a function to always have access to a private piece of state, you can use a closure.
In one of his videos (at around 1 min 25 seconds. The clock in the video goes backwards, so it's at -27:45), Douglas Crockford mentions that Javascript closures are a source of enormous expressive power and unlike other power constructs are also secure. He specifically mentions that in Javascript closures constraint scope, which makes them more secure.
Can someone help me with a few examples which show how scoping rules of Javascript closures make them more secure than other languages which have closures. Also are there any other things which make Javascript closures more secure than their counterparts in other languages?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With