2012-05-19 76 views
0

在Javascript中,如果我的功能之外,在JavaScript的VAR声明变量“var”全局定义变量的范围是什么?

var foo = 1; 
var bar = 2; 

function someFunction() { 
    .... 
} 

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

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

+2

[JavaScript变量范围(HTTP的可能重复://计算器.com/questions/500431/javascript-variable-scope) – Rodrigue

+0

在此处阅读。 HTTP://标记的故事。com/posts/view/picking-up-javascript-closures-and-lexical-scoping – atilkan

回答

1
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 

} 
+0

为什么是-1?这是正确的.... – Trevor

+0

我没有downvote,但我猜测-1是从你的第一个小试图回答这个问题。只是'var foo = 1; window.foo === foo;'真的不是那么好... – Andreas

+0

我不小心点击提交:P – Trevor

3

var将变量的作用域限制在它所定义的函数中,所以在顶层使用var定义的变量将实际上具有全局作用域。

如果您将值指定给未与var范围相关的变量,则无论您在何处定义它,它都会变为全局值。

有JavaScript的作用域的好文章在这里:What is the scope of variables in JavaScript?

+3

JavaScript中没有任何程序段范围。 – Andreas

+0

循环也没有确定范围。我编辑你的文章,以语义上纠正这一点。 –

+0

@web_bod这是不正确的。 JavaScript中的变量受它们所包含的函数的限制。'function foo(){if(1 == 1){var x =“xyz”; } console.log(x); }'会登录''xyz“'**涉及到已删除的评论** – Andreas

2

当你声明在JavaScript函数,它创建了一个范围。

当你声明一个变量时,它必须有一个varvar确定它属于哪个范围以及它在哪里可见。如果它没有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 
0

如JavaScript有唯一的功能范围,由var关键字定义的变量被限定在包含函数。

你可以很容易地通过在浏览器中打开JavaScript控制台(或直接在您的代码)检查全球范围界定,并键入:

varRef && console.log(varRef)