You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you don't plan on meeting such scripts you may even skip this chapter or postpone it, but then there's a chance that it bites you later.
20
+
如果你不打算遇到這樣的腳本,你甚至可以跳過這一章或者推遲閱讀,但是以後它可能會反咬你一口。
21
21
22
-
From the first sight, `var`behaves similar to `let`. That is, declares a variable:
22
+
從第一眼看來,`var`的行為與 `let` 相似。也就是說,它宣告了一個變數:
23
23
24
24
```js run
25
25
functionsayHi() {
26
-
var phrase ="Hello"; // local variable, "var" instead of "let"
26
+
var phrase ="Hello"; //局部變數 (local variable),使用 "var" 而非 "let"
27
27
28
28
alert(phrase); // Hello
29
29
}
@@ -33,74 +33,77 @@ sayHi();
33
33
alert(phrase); // Error, phrase is not defined
34
34
```
35
35
36
-
...But here are the differences.
36
+
...但是這裡有一些差異。
37
37
38
-
## "var" has no block scope
38
+
## "var" 沒有區塊作用域
39
39
40
40
Variables, declared with `var`, are either function-wide or global. They are visible through blocks.
41
+
使用 `var` 宣告的變數,其作用域不是函式範圍,就是全域範圍。它們可以在區塊中被看見。
41
42
42
-
For instance:
43
+
例如:
43
44
44
45
```js run
45
46
if (true) {
46
-
var test =true; //use "var" instead of "let"
47
+
var test =true; //使用 "var" 而非 "let"
47
48
}
48
49
49
50
*!*
50
-
alert(test); // true, the variable lives after if
51
+
alert(test); // true, 變數在 if 後仍存在
51
52
*/!*
52
53
```
53
54
54
-
As `var`ignores code blocks, we've got a global variable `test`.
55
+
`var`忽略了區塊,所以我們得到了一個全域變數 `test`。
55
56
56
-
If we used `let test`instead of `var test`, then the variable would only be visible inside `if`:
57
+
如果我們使用 `let test`而非 `var test`,那麼變數只會在 `if` 內可見:
57
58
58
59
```js run
59
60
if (true) {
60
-
let test =true; //use "let"
61
+
let test =true; //使用 "let"
61
62
}
62
63
63
64
*!*
64
65
alert(test); // Error: test is not defined
65
66
*/!*
66
67
```
67
68
68
-
The same thing for loops: `var`cannot be block- or loop-local:
69
+
對於迴圈也是一樣的:`var`不能是區塊或迴圈的區域變數:
69
70
70
71
```js
71
72
for (var i =0; i <10; i++) {
72
73
// ...
73
74
}
74
75
75
76
*!*
76
-
alert(i); // 10, "i" is visible after loop, it's a global variable
77
+
alert(i); // 10, "i" 在迴圈結束後仍存在,它是全域變數
77
78
*/!*
78
79
```
79
80
80
81
If a code block is inside a function, then `var` becomes a function-level variable:
82
+
如果一個區塊在函式內部,那麼 `var` 就會變成函式級別的變數:
81
83
82
84
```js run
83
85
functionsayHi() {
84
86
if (true) {
85
87
var phrase ="Hello";
86
88
}
87
89
88
-
alert(phrase); //works
90
+
alert(phrase); //可行
89
91
}
90
92
91
93
sayHi();
92
94
alert(phrase); // Error: phrase is not defined (Check the Developer Console)
93
95
```
94
96
95
97
As we can see, `var` pierces through `if`, `for` or other code blocks. That's because a long time ago in JavaScript blocks had no Lexical Environments. And `var` is a remnant of that.
## "var" declarations are processed at the function start
100
+
## "var" 宣告在函式開始時處理
98
101
99
-
`var`declarations are processed when the function starts (or script starts for globals).
102
+
`var`宣告在函式開始時處理 (或全域範圍開始時處理)。
100
103
101
-
In other words, `var`variables are defined from the beginning of the function, no matter where the definition is (assuming that the definition is not in the nested function).
104
+
換句話說,`var`變數在函式開始時就被定義了,不管它的定義在哪裡 (假設定義不在巢狀函式中)。
102
105
103
-
So this code:
106
+
這段程式碼:
104
107
105
108
```js run
106
109
functionsayHi() {
@@ -115,7 +118,7 @@ function sayHi() {
115
118
sayHi();
116
119
```
117
120
118
-
...Is technically the same as this (moved`var phrase`above):
121
+
...技術上與這個程式碼相同 (將`var phrase`移到上面):
119
122
120
123
```js run
121
124
functionsayHi() {
@@ -157,7 +160,7 @@ That's best demonstrated with an example:
157
160
158
161
```js run
159
162
functionsayHi() {
160
-
alert(phrase);
163
+
alert(phrase);
161
164
162
165
*!*
163
166
var phrase ="Hello";
@@ -203,12 +206,10 @@ That's not something we should use nowadays, but you can find them in old script
0 commit comments