javascript variable's scope , 变量 范围

来源:互联网 发布:淘宝如何分类宝贝 编辑:程序博客网 时间:2024/05/29 15:30

Detailed example that writes to this page: 

You can reference all local variables inside the same function the variables are declared.

(L1) local variable declared with var keyword

(L2) local variable declared without var keyword

(L3) local variable declared with var keyword inside if statement

 

You can reference all global variables inside any function.

(G1) global variable declared with var keyword

(G2) global variable without var keyword

(G3) global variable declared with var keyword inside if statement

 

Outside a function, you can only reference local variables that are not declared with the var keyword.

Commented out. Cannot reference (L1)

(L2) local variable declared without var keyword

Commented out. Cannot reference (L3)

 

Outside a function, you can reference all global variables.

(G1) global variable declared with var keyword

(G2) global variable without var keyword

(G3) global variable declared with var keyword inside if statement

原创粉丝点击