javascript - Why does getValue() return 123?
伊谢尔伦
伊谢尔伦 2017-06-26 10:57:34
0
2
669
    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
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(2)
世界只因有你

getValue() and setValue() are closures, sharing a variable secret, so if the secret is changed in the setValue function, getValue() will of course read the secret Change accordingly.

淡淡烟草味

Isn’t it 0? How did you get 123.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template