var getValue,setValue;
(function(){
var secret=0;
getValue=function(){
return secret;
};
setValue=function(v){
if(typeof v==="number"){
secret=v;
}
};
}());
getValue();//0
setValue(123);
getValue();//123
setValue(false);
getValue();//123
getValue()
andsetValue()
are closures, sharing a variablesecret
, so if thesecret
is changed in thesetValue
function,getValue()
will of course read thesecret
Change accordingly.Isn’t it 0? How did you get 123.