Are variables statically or dynamically “scoped” in javascript?

前端 未结 7 1913
無奈伤痛
無奈伤痛 2020-12-13 18:38

Or more specific to what I need:

If I call a function from within another function, is it going to pull the variable from within the calling function, or from the le

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-13 19:14

    myVar=0;
    
    function runMe(){
        myVar = 10;
        callMe();
    }
    
    function callMe(){
       addMe = myVar+10;
    }
    

    As far as the output is concerned myVar and addMe both will be global variable in this case , as in javascript if you don't declare a variable with var then it implicitly declares it as global hence when you call runMe() then myVar will have the value 10 and addMe will have 20 .

提交回复
热议问题