closures are the primary mechanism used to enable data privacy. When you use closures for data privacy, the enclosed variables are only in scope within the containing (outer) function. You can’t get at the data from an outside scope except through the object’s privileged methods. In JavaScript, any exposed method defined within the closure scope is privileged.
In Javascript variable declared outside the function are injectable inside the function due to the closure. Below is a simple example of closure var a ='hey';function test(){console.log(a); }test()
function showName (firstName, lastName) {? var nameIntro = "Your name is ";// this inner function has access to the outer function's variables, including the parameter function makeFullName () {? return nameIntro + firstName + " " + lastName;? }return makeFullName ();? }?showName ("Michael", "Jackson"); // Your name is Michael Jackson?
http://javascriptissexy.com/understand-javascript-closures-with-ease/