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.
Interviews6 answers
What are JavaScript closures?
6 Answers
Bidyasagar MishraPosted Aug 1, 2019, 9:29 PM EDT
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()
Neeraj NegiPosted Oct 7, 2018, 11:11 PM EDT
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?
Lalit RaghavPosted Aug 18, 2018, 6:51 AM EDT
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?
Lalit RaghavPosted Aug 18, 2018, 6:51 AM EDT
http://javascriptissexy.com/understand-javascript-closures-with-ease/
Lalit RaghavPosted Aug 18, 2018, 6:51 AM EDT
http://javascriptissexy.com/understand-javascript-closures-with-ease/
Lalit RaghavPosted Aug 18, 2018, 6:51 AM EDT


