- 1

What does closure exactly mean in Javascript?

I don't want bookish explanation.

31st Aug 2017, 2:30 AM
Ajit Fawade
Ajit Fawade - avatar
2 Respuestas
+ 6
In few words, a closure is a private variable space, not accessible from outside of it... In JS, mostly closure are created when you open curly brackets to enclose instruction for a function definition: var A = 42; // defined in the global scope, wich can be considered as root closure function test() { // creating function create a private context (the closure) var B = "forty-two"; // defined in the function scope (the function private closure) console.log(A); // output '42', because global scope always accessible (always in the parent closures tree) console.log(B); // output 'forty-two', because we are in the scope of B } console.log(A); // output '42' (we are in the global scope / same closure) console.log(B); // 'undefined', because the test() function scope isn't accessible from outside
31st Aug 2017, 9:27 AM
visph
visph - avatar
+ 3
O I never knew something like that even existed
31st Aug 2017, 9:30 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar