Are variables statically or dynamically “scoped” i

2019-02-01 18:09发布

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 level above? Ex:

myVar=0;

function runMe(){
    myVar = 10;
    callMe();
}

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

What does myVar end up being if callMe() is called through runMe()?

7条回答
迷人小祖宗
2楼-- · 2019-02-01 18:45

Unless you use the keyword var to define your variables, everything ends up being a property on the window object. So your code would be equivalent to the following:

window.myVar=0;

function runMe(){
    window.myVar = 10;
    window.callMe();
}

function callMe(){
   window.addMe = window.myVar+10;
}

If you keep this in mind, it should always be clear what is happening.

查看更多
戒情不戒烟
3楼-- · 2019-02-01 18:48

Variables are statically scoped in JavaScript (dynamic scoping is really a pretty messy business: you can read more about it on Wikipedia).

In your case though, you're using a global variable, so all functions will access that same variable. Matthew Flaschen's reply shows how you can change it so the second myVar is actually a different variable.

This Page explains how to declare global vs. local variables in JavaScript, in case you're not too familiar with it. It's different from the way most scripting languages do it. (In summary: the "var" keyword makes a variable local if declared inside a function, otherwise the variable is global.)

查看更多
Deceive 欺骗
4楼-- · 2019-02-01 18:49

As far as I understand, any variable without the var keyword is treated global, with it, its local scoped, so:

// This is a local scoped variable.
var local_var = "something"; 

// This is a global scoped variable.
global_var = "something_else";

As a good JS practice, it is recommended to ALWAYS add the var keyword.

查看更多
戒情不戒烟
5楼-- · 2019-02-01 18:53
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 .

查看更多
做自己的国王
6楼-- · 2019-02-01 19:01

I would like to add that lambda expressions are also statically scoped at the location that the expression is defined. For example,

var myVar = 0;

function foo() {
    var myVar = 10;
    return { bar: function() { addMe = myVar + 10; }}
}

var myObj = foo();

var addMe = 6;
alert(addMe);

myVar = 42;
myObj.bar();

alert(addMe);

This will display 6 and 20.

查看更多
成全新的幸福
7楼-- · 2019-02-01 19:04

if your next line is callMe();, then addMe will be 10, and myVar will be 0.

if your next line is runMe();, then addMe will be 20, and myVar will be 10.

Forgive me for asking - what does this have to do with static/dynamic binding? Isn't myVar simply a global variable, and won't the procedural code (unwrap everything onto the call stack) determine the values?

查看更多
登录 后发表回答