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 a code block is inside a function, then `var` becomes a function-level variable:
82
-
如果一個區塊在函式內部,那麼 `var`就會變成函式級別的變數:
82
+
如果一個區塊在函式內部,那麼 `var`就會變成函式層級的變數:
83
83
84
84
```js run
85
85
functionsayHi() {
@@ -91,7 +91,7 @@ function sayHi() {
91
91
}
92
92
93
93
sayHi();
94
-
alert(phrase); // Error: phrase is not defined (Check the Developer Console)
94
+
alert(phrase); // Error: phrase is not defined (可透過開發者工具查看)
95
95
```
96
96
97
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.
@@ -133,7 +133,7 @@ function sayHi() {
133
133
sayHi();
134
134
```
135
135
136
-
...Or even as this (remember, code blocks are ignored):
136
+
...或者這樣 (記住,區塊是被忽略的):
137
137
138
138
```js run
139
139
functionsayHi() {
@@ -150,13 +150,13 @@ function sayHi() {
150
150
sayHi();
151
151
```
152
152
153
-
People also call such behavior "hoisting" (raising), because all `var`are "hoisted" (raised) to the top of the function.
So in the example above, `if (false)`branch never executes, but that doesn't matter. The `var`inside it is processed in the beginning of the function, so at the moment of `(*)`the variable exists.
**Declarations are hoisted, but assignments are not.**
157
+
**宣告 (declarations) 會被提升,但賦值 (assignments) 不會。**
158
158
159
-
That's best demonstrated with an example:
159
+
這個例子可以很好地說明:
160
160
161
161
```js run
162
162
functionsayHi() {
@@ -170,40 +170,40 @@ function sayHi() {
170
170
sayHi();
171
171
```
172
172
173
-
The line `var phrase = "Hello"`has two actions in it:
173
+
`var phrase = "Hello"`這一行有兩個動作:
174
174
175
-
1.Variable declaration`var`
176
-
2.Variable assignment`=`.
175
+
1.變數宣告`var`
176
+
2.變數賦值`=`.
177
177
178
-
The declaration is processed at the start of function execution ("hoisted"), but the assignment always works at the place where it appears. So the code works essentially like this:
178
+
宣告在函式開始時處理 (提升),但賦值總是在它出現的地方處理。所以程式碼實際上像這樣運作:
179
179
180
180
```js run
181
181
functionsayHi() {
182
182
*!*
183
-
var phrase; //declaration works at the start...
183
+
var phrase; //宣告在函式開始時處理...
184
184
*/!*
185
185
186
186
alert(phrase); // undefined
187
187
188
188
*!*
189
-
phrase ="Hello"; // ...assignment - when the execution reaches it.
189
+
phrase ="Hello"; // ...賦值 - 當程式執行到這裡時
190
190
*/!*
191
191
}
192
192
193
193
sayHi();
194
194
```
195
195
196
-
Because all `var`declarations are processed at the function start, we can reference them at any place. But variables are undefined until the assignments.
As in the past there was only `var`, and it has no block-level visibility, programmers invented a way to emulate it. What they did was called "immediately-invoked function expressions" (abbreviated as IIFE).
202
+
過去只有 `var`,而且它沒有區塊層級的可見性,所以程式設計師發明了一種模擬它的方式,被稱為 "立即呼叫函式 (immediately-invoked function expressions)"。
203
203
204
-
That's not something we should use nowadays, but you can find them in old scripts.
204
+
這不是我們現在應該使用的東西,但你可以在舊的腳本中找到它們。
205
205
206
-
An IIFE looks like this:
206
+
一個 IIFE 看起來像這樣:
207
207
208
208
```js run
209
209
(function () {
@@ -213,12 +213,12 @@ An IIFE looks like this:
213
213
})();
214
214
```
215
215
216
-
Here a Function Expression is created and immediately called. So the code executes right away and has its own private variables.
The Function Expression is wrapped with parenthesis `(function {...})`, because when JavaScript meets`"function"`in the main code flow, it understands it as the start of a Function Declaration. But a Function Declaration must have a name, so this kind of code will give an error:
}(); // <-- can't call Function Declaration immediately
237
+
}(); // <-- 不能立即呼叫函式宣告
238
238
```
239
239
240
-
So, the parentheses around the function is a trick to show JavaScript that the function is created in the context of another expression, and hence it's a Function Expression: it needs no name and can be called immediately.
There exist other ways besides parentheses to tell JavaScript that we mean a Function Expression:
242
+
除了括號之外,還有其他方法可以告訴 JavaScript 我們的意思是函式表達式:
243
243
244
244
```js run
245
-
//Ways to create IIFE
245
+
//建立 IIFE 的其他方式
246
246
247
247
(function() {
248
248
alert("Parentheses around the function");
@@ -261,15 +261,15 @@ There exist other ways besides parentheses to tell JavaScript that we mean a Fun
261
261
}();
262
262
```
263
263
264
-
In all the above cases we declare a Function Expression and run it immediately. Let's note again: nowadays there's no reason to write such code.
264
+
在上述所有情況下,我們都宣告了一個函式表達式並立即執行它。再次注意:現在沒有理由寫這樣的程式碼。
265
265
266
-
## Summary
266
+
## 結論
267
267
268
-
There are two main differences of `var` compared to `let/const`:
268
+
相較於 `let/const`,`var` 有兩個主要的差異:
269
269
270
-
1.`var`variables have no block scope, they are visible minimum at the function level.
271
-
2.`var`declarations are processed at function start (script start for globals).
270
+
1.`var`變數沒有區塊區塊作用域,它們至少在函式層級可見。
271
+
2.`var`宣告在函式開始時處理 (全域範圍開始時處理)。
272
272
273
-
There's one more very minor difference related to the global object, that we'll cover in the next chapter.
273
+
還有一個與全域物件 (global object) 有關的非常小的差異,我們將在下一章中介紹。
274
274
275
-
These differences make `var`worse than `let`most of the time. Block-level variables is such a great thing. That's why `let`was introduced in the standard long ago, and is now a major way (along with `const`) to declare a variable.
0 commit comments