【问题标题】:What scope are variables globally defined by "var"?“var”全局定义的变量范围是什么?
【发布时间】:2012-05-26 21:37:55
【问题描述】:

在 Javascript 中,如果我在函数之外的 javascript 中使用 var 声明变量

var foo = 1;
var bar = 2;

function someFunction() {
    ....
}

这些变量是否在文档或窗口的范围内?此外,为什么这很重要?我知道如果一个人声明一个没有var的变量,那么这个变量就是全局的。

有没有一种简单的方法可以让我测试一个变量是否属于文档或窗口的范围?

【问题讨论】:

标签: javascript


【解决方案1】:

var 将变量的范围限制为定义它的函数,因此在顶层使用 var 定义的变量将有效地具有全局范围。

如果您为未使用 var 限定范围的变量分配值,那么无论您在何处定义它,它都会变为全局变量。

这里有一篇关于 javascript 作用域的好文章:What is the scope of variables in JavaScript?

【讨论】:

  • JavaScript 中没有块作用域。
  • 循环也没有作用域。我编辑了您的帖子以在语义上更正此问题。
  • @web_bod 这是不正确的。 JavaScript 中的变量受包含它们的函数的限制。function foo() {if (1 == 1) { var x = "xyz"; } console.log(x); } 将记录 "xyz" 与已删除的评论相关
【解决方案2】:
var foo = 1;

window.foo === foo;

JavaScript 是一种函数式语言,因此在函数范围内声明的任何变量都只能在该函数中使用。

JS 实际上会遍历每个函数的作用域并查找声明的变量。

function setGlobal() {
    bar = 1; // gets set as window.bar because setGlobal does not define it
}
setGlobal();

// logs true and 1
console.log(window.bar === bar, bar); ​

http://jsfiddle.net/kXjrF/

所以...

function logGlobal() {
    var bar;
    console.log( foo, window.foo ) // undefined, undefined
    function setGlobal() {
        // window.foo is now set because logGlobal did not define foo
        foo = 1;  
        bar = 2; // logGlobal's bar not window.bar
        function makePrivate() {
           var foo = 3; // local foo
           console.log( foo ); // logs 3
        }
        makePrivate(); // logs 3
    }
    setGlobal();
    console.log( foo, window.foo ); // logs 1, 1

}

【讨论】:

  • 我没有投反对票,但我猜想 -1 是您第一次尝试回答这个问题。只是var foo = 1; window.foo === foo; 一点都不好...
【解决方案3】:

当你在 JavaScript 中声明一个函数时,它会创建一个作用域。

当你声明一个变量时,它必须有一个var。 var 确定它所属的范围以及可见的位置。如果它没有var,那么它是对变量的“赋值”,并且浏览器假定具有该名称的变量存在于外部作用域中。

当分配发生时,浏览器向外搜索直到到达全局范围。如果浏览器在全局范围内没有看到赋值的变量,就会在全局范围内声明(这样不好)

例如,将以下内容作为范围可见性的演示,不是实际的工作功能:

//global variables
var w = 20
var x = 10

function  foo(){
    function bar(){

        //we assign x. since it's not declared with var
        //the browser looks for x in the outer scopes
        x = 0;

        function baz(){

            //we can still see and edit x here, turning it from 0 to 1
            x = 1;

            //redeclaring a variable makes it a local variable
            //it does not affect the variable of the same name outside
            //therefore within baz, w is 13 but outside, it's still 20
            var w = 13;

            //declaring y inside here, it does not exist in the outer scopes
            //therefore y only exists in baz
            var y = 2;

            //failing to use var makes the browser look for the variable outside
            //if there is none, the browser declares it as a global                
            z = 3;
        }
    }
}

//w = 20         - since the w inside was "redeclared" inside 
//x = 1          - changed since all x operations were assigments
//y = undefined  - never existed in the outside 
//z = 3          - was turned into a global

【讨论】:

    【解决方案4】:

    由于 JavaScript 只有函数作用域,var 关键字定义的变量的作用域是包含函数。

    您可以通过在浏览器中(或直接在您的代码中)打开 JavaScript 控制台并输入以下内容来轻松检查全局范围:

    varRef && console.log(varRef)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-25
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      • 2015-01-30
      • 2016-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多