-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Akin C edited this page Jun 27, 2017
·
9 revisions
Closures are not supported in all programming languages, but they are in Javascript. It seems to understand them, it is necessary to look at three facts about closures.
This is the third one and the others can be found here:
Fact three says that a variable which is defined in an outer function can be updated. For example
function anyFunction()
{
//Similar to a member variable
var varOutside = undefined;
return {
//Similar to methods
setValue: function(newValue)
{
varOutside = newValue;
},
getValue: function()
{
return varOutside;
}
};
}
var myObject = anyFunction();
myObject.setValue(1000);
console.log( myObject.getValue() );//Outputs 1000STILL IN WORK(for longer)!!
This knowledge was gained:
- Effective JavaScript "68 Specific Ways to Harness the Power of JavaScript" by David Herman
Sources: