2012-05-19 75 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)