Skip to content

Commit e6159b9

Browse files
committed
Update:Adding transaltion for article the old var
1 parent 9c919ba commit e6159b9

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

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

+30-29
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
21
# The old "var"
32

4-
```smart header="This article is for understanding old scripts"
5-
The information in this article is useful for understanding old scripts.
3+
```smart header="本篇文章主要是幫助理解舊的腳本"
4+
本篇文章的資訊對於理解舊的腳本很有幫助。
65
7-
That's not how we write a new code.
6+
但這不是我們寫新程式碼的方式
87
```
98

109
In the very first chapter about [variables](info:variables), we mentioned three ways of variable declaration:
10+
[變數](info:variables)的第一章中,我們提到了三種變數宣告的方式:
1111

1212
1. `let`
1313
2. `const`
1414
3. `var`
1515

16-
`let` and `const` behave exactly the same way in terms of Lexical Environments.
16+
在作用域環境 (Lexical Environments) 方面,`let` `const` 的行為完全相同。
1717

18-
But `var` is a very different beast, that originates from very old times. It's generally not used in modern scripts, but still lurks in the old ones.
18+
`var` 是一個非常不同的東西,它來自很久以前的時代。它通常不會在現代腳本中使用,但仍然潛伏在舊腳本中。
1919

20-
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+
如果你不打算遇到這樣的腳本,你甚至可以跳過這一章或者推遲閱讀,但是以後它可能會反咬你一口。
2121

22-
From the first sight, `var` behaves similar to `let`. That is, declares a variable:
22+
從第一眼看來,`var` 的行為與 `let` 相似。也就是說,它宣告了一個變數:
2323

2424
```js run
2525
function sayHi() {
26-
var phrase = "Hello"; // local variable, "var" instead of "let"
26+
var phrase = "Hello"; // 局部變數 (local variable),使用 "var" 而非 "let"
2727

2828
alert(phrase); // Hello
2929
}
@@ -33,74 +33,77 @@ sayHi();
3333
alert(phrase); // Error, phrase is not defined
3434
```
3535

36-
...But here are the differences.
36+
...但是這裡有一些差異。
3737

38-
## "var" has no block scope
38+
## "var" 沒有區塊作用域
3939

4040
Variables, declared with `var`, are either function-wide or global. They are visible through blocks.
41+
使用 `var` 宣告的變數,其作用域不是函式範圍,就是全域範圍。它們可以在區塊中被看見。
4142

42-
For instance:
43+
例如:
4344

4445
```js run
4546
if (true) {
46-
var test = true; // use "var" instead of "let"
47+
var test = true; // 使用 "var" 而非 "let"
4748
}
4849

4950
*!*
50-
alert(test); // true, the variable lives after if
51+
alert(test); // true, 變數在 if 後仍存在
5152
*/!*
5253
```
5354

54-
As `var` ignores code blocks, we've got a global variable `test`.
55+
`var` 忽略了區塊,所以我們得到了一個全域變數 `test`
5556

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` 內可見:
5758

5859
```js run
5960
if (true) {
60-
let test = true; // use "let"
61+
let test = true; // 使用 "let"
6162
}
6263

6364
*!*
6465
alert(test); // Error: test is not defined
6566
*/!*
6667
```
6768

68-
The same thing for loops: `var` cannot be block- or loop-local:
69+
對於迴圈也是一樣的:`var` 不能是區塊或迴圈的區域變數:
6970

7071
```js
7172
for (var i = 0; i < 10; i++) {
7273
// ...
7374
}
7475

7576
*!*
76-
alert(i); // 10, "i" is visible after loop, it's a global variable
77+
alert(i); // 10, "i" 在迴圈結束後仍存在,它是全域變數
7778
*/!*
7879
```
7980

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

8284
```js run
8385
function sayHi() {
8486
if (true) {
8587
var phrase = "Hello";
8688
}
8789

88-
alert(phrase); // works
90+
alert(phrase); // 可行
8991
}
9092

9193
sayHi();
9294
alert(phrase); // Error: phrase is not defined (Check the Developer Console)
9395
```
9496

9597
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.
98+
如同我們所見,`var` 穿透了 `if``for` 或其他區塊。這是因為在 JavaScript 中,區塊在很久以前沒有作用域環境 (Lexical Environments)。而 `var` 就是那個時代的遺物。
9699

97-
## "var" declarations are processed at the function start
100+
## "var" 宣告在函式開始時處理
98101

99-
`var` declarations are processed when the function starts (or script starts for globals).
102+
`var` 宣告在函式開始時處理 (或全域範圍開始時處理)。
100103

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` 變數在函式開始時就被定義了,不管它的定義在哪裡 (假設定義不在巢狀函式中)。
102105

103-
So this code:
106+
這段程式碼:
104107

105108
```js run
106109
function sayHi() {
@@ -115,7 +118,7 @@ function sayHi() {
115118
sayHi();
116119
```
117120

118-
...Is technically the same as this (moved `var phrase` above):
121+
...技術上與這個程式碼相同 (將 `var phrase` 移到上面):
119122

120123
```js run
121124
function sayHi() {
@@ -157,7 +160,7 @@ That's best demonstrated with an example:
157160

158161
```js run
159162
function sayHi() {
160-
alert(phrase);
163+
alert(phrase);
161164

162165
*!*
163166
var phrase = "Hello";
@@ -203,12 +206,10 @@ That's not something we should use nowadays, but you can find them in old script
203206
An IIFE looks like this:
204207

205208
```js run
206-
(function() {
207-
209+
(function () {
208210
let message = "Hello";
209211

210212
alert(message); // Hello
211-
212213
})();
213214
```
214215

0 commit comments

Comments
 (0)