Skip to content

Commit ddff0a1

Browse files
committed
Update:Complete translation of the article the old var
1 parent e6159b9 commit ddff0a1

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

Diff for: 1-js/06-advanced-functions/04-var/article.md

+34-34
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ alert(i); // 10, "i" 在迴圈結束後仍存在,它是全域變數
7979
```
8080

8181
If a code block is inside a function, then `var` becomes a function-level variable:
82-
如果一個區塊在函式內部,那麼 `var` 就會變成函式級別的變數
82+
如果一個區塊在函式內部,那麼 `var` 就會變成函式層級的變數
8383

8484
```js run
8585
function sayHi() {
@@ -91,7 +91,7 @@ function sayHi() {
9191
}
9292

9393
sayHi();
94-
alert(phrase); // Error: phrase is not defined (Check the Developer Console)
94+
alert(phrase); // Error: phrase is not defined (可透過開發者工具查看)
9595
```
9696

9797
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() {
133133
sayHi();
134134
```
135135

136-
...Or even as this (remember, code blocks are ignored):
136+
...或者這樣 (記住,區塊是被忽略的):
137137

138138
```js run
139139
function sayHi() {
@@ -150,13 +150,13 @@ function sayHi() {
150150
sayHi();
151151
```
152152

153-
People also call such behavior "hoisting" (raising), because all `var` are "hoisted" (raised) to the top of the function.
153+
人們也稱這種行為作 "hoisting" (提升),因為所有的 `var` 都被 "hoisted" (提升) 到函式的頂部。
154154

155-
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.
155+
因此在上面的例子中,`if (false)` 分支永遠不會執行,但這並不重要。它內部的 `var` 在函式開始時就被處理了,所以在 `(*)` 的時候變數已經存在了。
156156

157-
**Declarations are hoisted, but assignments are not.**
157+
**宣告 (declarations) 會被提升,但賦值 (assignments) 不會。**
158158

159-
That's best demonstrated with an example:
159+
這個例子可以很好地說明:
160160

161161
```js run
162162
function sayHi() {
@@ -170,40 +170,40 @@ function sayHi() {
170170
sayHi();
171171
```
172172

173-
The line `var phrase = "Hello"` has two actions in it:
173+
`var phrase = "Hello"` 這一行有兩個動作:
174174

175-
1. Variable declaration `var`
176-
2. Variable assignment `=`.
175+
1. 變數宣告 `var`
176+
2. 變數賦值 `=`.
177177

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+
宣告在函式開始時處理 (提升),但賦值總是在它出現的地方處理。所以程式碼實際上像這樣運作:
179179

180180
```js run
181181
function sayHi() {
182182
*!*
183-
var phrase; // declaration works at the start...
183+
var phrase; // 宣告在函式開始時處理...
184184
*/!*
185185

186186
alert(phrase); // undefined
187187

188188
*!*
189-
phrase = "Hello"; // ...assignment - when the execution reaches it.
189+
phrase = "Hello"; // ...賦值 - 當程式執行到這裡時
190190
*/!*
191191
}
192192

193193
sayHi();
194194
```
195195

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.
196+
因為所有的 `var` 宣告都在函式開始時處理,所以我們可以在任何地方引用它們。但是變數在賦值之前都是未定義的。
197197

198-
In both examples above `alert` runs without an error, because the variable `phrase` exists. But its value is not yet assigned, so it shows `undefined`.
198+
在上面的兩個例子中,`alert` 都沒有錯誤,因為變數 `phrase` 存在。但它的值還沒有被賦值,所以它顯示 `undefined`
199199

200200
### IIFE
201201

202-
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)"。
203203

204-
That's not something we should use nowadays, but you can find them in old scripts.
204+
這不是我們現在應該使用的東西,但你可以在舊的腳本中找到它們。
205205

206-
An IIFE looks like this:
206+
一個 IIFE 看起來像這樣:
207207

208208
```js run
209209
(function () {
@@ -213,12 +213,12 @@ An IIFE looks like this:
213213
})();
214214
```
215215

216-
Here a Function Expression is created and immediately called. So the code executes right away and has its own private variables.
216+
這裡建立了一個函式表達式 (Function Expression) 並立即呼叫它。所以程式碼立即執行,並且有它自己的私有變數。
217217

218-
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:
218+
函式表達式被括號 `(function {...})` 包裹,因為當 JavaScript 在主程式碼中遇到 `"function"` 時,它會將其視為函式宣告的開始。但是函式宣告必須有名字,所以這種程式碼會出錯:
219219

220220
```js run
221-
// Try to declare and immediately call a function
221+
// 嘗試宣告並立即呼叫函式
222222
function() { // <-- Error: Function statements require a function name
223223

224224
let message = "Hello";
@@ -228,21 +228,21 @@ function() { // <-- Error: Function statements require a function name
228228
}();
229229
```
230230

231-
Even if we say: "okay, let's add a name", that won't work, as JavaScript does not allow Function Declarations to be called immediately:
231+
即使我們說:"好吧,那就加一個名字",但這也不會起作用,因為 JavaScript 不允許立即呼叫函式宣告:
232232

233233
```js run
234-
// syntax error because of parentheses below
234+
// 因為下面的括號而導致語法錯誤
235235
function go() {
236236

237-
}(); // <-- can't call Function Declaration immediately
237+
}(); // <-- 不能立即呼叫函式宣告
238238
```
239239

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.
240+
因此,函式周圍的括號是一個技巧,可以告訴 JavaScript 函式是在另一個表達式的上下文中建立的,因此它是一個函式表達式:它不需要名字,並且可以立即呼叫。
241241

242-
There exist other ways besides parentheses to tell JavaScript that we mean a Function Expression:
242+
除了括號之外,還有其他方法可以告訴 JavaScript 我們的意思是函式表達式:
243243

244244
```js run
245-
// Ways to create IIFE
245+
// 建立 IIFE 的其他方式
246246

247247
(function() {
248248
alert("Parentheses around the function");
@@ -261,15 +261,15 @@ There exist other ways besides parentheses to tell JavaScript that we mean a Fun
261261
}();
262262
```
263263

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+
在上述所有情況下,我們都宣告了一個函式表達式並立即執行它。再次注意:現在沒有理由寫這樣的程式碼。
265265

266-
## Summary
266+
## 結論
267267

268-
There are two main differences of `var` compared to `let/const`:
268+
相較於 `let/const``var` 有兩個主要的差異:
269269

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` 宣告在函式開始時處理 (全域範圍開始時處理)。
272272

273-
There's one more very minor difference related to the global object, that we'll cover in the next chapter.
273+
還有一個與全域物件 (global object) 有關的非常小的差異,我們將在下一章中介紹。
274274

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.
275+
這些差異使得 `var` 大多數時候比 `let` 差。區塊層級的變數是一件很棒的事情。這就是為什麼 `let` 很久以前就被引入標準中,並且現在是宣告變數的主要方式之一 (與 `const` 一起)。

0 commit comments

Comments
 (0)