简体   繁体   中英

JS Revealing Module private variable value issue

I've got stuck with this issue.

var myModule = (function(){

    var foo;

    foo = "bab"; 
    console.log(foo);

    foo = "bab" + "more";
    console.log(foo);


    function mth1(){
      foo = "mag";
      console.log(foo);
    }

    return{
      pubMethod1: mth1 
    };

})();

myModule.pubMethod1();

The problem is that instead of this output:

"bab"
"bab more"
"mag"

I get this output:

"bab"
"mag"
"mag"

More specifically, the "bab more" is for some reason overwritten by mth1() value.

Original code publishing is not available.

Your thoughts much appreciated.

Part of the problem, apart the original code bug, was console.log issue.

More info about here at StackOverflow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM