Skip to content

Commit 4a26176

Browse files
author
howardchi
committed
finished function/ closures, general
1 parent 151456f commit 4a26176

File tree

2 files changed

+42
-66
lines changed

2 files changed

+42
-66
lines changed

doc/zh-TW/function/closures.md

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
## Closures and References
1+
## Closures References
22

3-
One of JavaScript's most powerful features is the availability of *closures*.
4-
With closures, scopes **always** keep access to the outer scope, in which they
5-
were defined. Since the only scoping that JavaScript has is
6-
[function scope](#function.scopes), all functions, by default, act as closures.
3+
JavaScript 有一個很重要的特徵就是 **closures**
4+
因為有 Closures,所以作用域 **永遠** 能夠去訪問作用區間外面的變數。
5+
[函數區間](#function.scopes) 是JavaScript 中唯一擁有自生作用域的結構,因此 Closures 的創立需要依賴函數
76

8-
### Emulating private variables
7+
### 模仿私有變數
98

109
function Counter(start) {
1110
var count = start;
@@ -24,51 +23,39 @@ were defined. Since the only scoping that JavaScript has is
2423
foo.increment();
2524
foo.get(); // 5
2625

27-
Here, `Counter` returns **two** closures: the function `increment` as well as
28-
the function `get`. Both of these functions keep a **reference** to the scope of
29-
`Counter` and, therefore, always keep access to the `count` variable that was
30-
defined in that scope.
26+
這裡,`Counter` 返回兩個 Closures,函數 `increment` 還有 `get`。這兩個函數都維持著對外部作用域 `Counter` 的引用,因此總可以訪問作用域的變數 `count`
3127

32-
### Why Private Variables Work
3328

34-
Since it is not possible to reference or assign scopes in JavaScript, there is
35-
**no** way of accessing the variable `count` from the outside. The only way to
36-
interact with it is via the two closures.
29+
### 為什麼不可以在外部訪問私有變數
30+
31+
因為 Javascript **不可以** 對作用域進行引用或賦值。因此外部的地方沒有辦法訪問 `count` 變數。
32+
唯一的途徑就是經過那兩個 Closures
3733

3834
var foo = new Counter(4);
3935
foo.hack = function() {
4036
count = 1337;
4137
};
4238

43-
The above code will **not** change the variable `count` in the scope of `Counter`,
44-
since `foo.hack` was not defined in **that** scope. It will instead create - or
45-
override - the *global* variable `count`.
39+
在上面的例子中 `count` **不會** 改變到 `Counter` 裡面的 `count` 的值。因為 `foo.hack` 沒有在 **那個** 作用域內被宣告。它只有會覆蓋或者建立在一個 **全域** 的變數 `count`
4640

47-
### Closures Inside Loops
41+
### 在循環內的 Closures
4842

49-
One often made mistake is to use closures inside of loops, as if they were
50-
copying the value of the loop's index variable.
43+
一個常見的錯誤就是在 Closures 中使用迴圈,假設我們要使用每次迴圈中所使用的進入變數
5144

5245
for(var i = 0; i < 10; i++) {
5346
setTimeout(function() {
5447
console.log(i);
5548
}, 1000);
5649
}
5750

58-
The above will **not** output the numbers `0` through `9`, but will simply print
59-
the number `10` ten times.
60-
61-
The *anonymous* function keeps a **reference** to `i`. At the time
62-
`console.log` gets called, the `for loop` has already finished, and the value of
63-
`i` as been set to `10`.
64-
65-
In order to get the desired behavior, it is necessary to create a **copy** of
66-
the value of `i`.
51+
在上面的例子中它 **不會** 輸出數字從 `0``9`,但只會出現數字 `10` 十次。
52+
`console.log` 被呼叫的時候,這個 *匿名* 函數中保持一個 **參考** 到 i ,此時 `for`迴圈已經結束, `i` 的值被修改成了 `10`
53+
為了要達到想要的結果,需要在每次創造 **副本** 來儲存 `i` 的變數。
6754

68-
### Avoiding the Reference Problem
55+
### 避免引用錯誤
6956

70-
In order to copy the value of the loop's index variable, it is best to use an
71-
[anonymous wrapper](#function.scopes).
57+
為了要有達到正確的效果,最好是把它包在一個
58+
[匿名函數](#function.scopes).
7259

7360
for(var i = 0; i < 10; i++) {
7461
(function(e) {
@@ -78,15 +65,9 @@ In order to copy the value of the loop's index variable, it is best to use an
7865
})(i);
7966
}
8067

81-
The anonymous outer function gets called immediately with `i` as its first
82-
argument and will receive a copy of the **value** of `i` as its parameter `e`.
83-
84-
The anonymous function that gets passed to `setTimeout` now has a reference to
85-
`e`, whose value does **not** get changed by the loop.
86-
87-
There is another possible way of achieving this, which is to return a function
88-
from the anonymous wrapper that will then have the same behavior as the code
89-
above.
68+
匿名外部的函數被呼叫,並把 `i` 作為它第一個參數,此時函數內 `e` 變數就擁有了一個 `i` 的拷貝。
69+
當傳遞給 `setTimeout` 這個匿名函數執行時,它就擁有了對 `e` 的引用,而這個值 **不會** 被循環改變。
70+
另外有一個方法也可以完成這樣的工作,那就是在匿名函數中返回一個函數,這和上面的程式碼有同樣的效果。
9071

9172
for(var i = 0; i < 10; i++) {
9273
setTimeout((function(e) {

doc/zh-TW/function/general.md

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,43 @@
1-
## Function Declarations and Expressions
1+
## 函式的宣告和表達方式
22

3-
Functions in JavaScript are first class objects. That means they can be
4-
passed around like any other value. One common use of this feature is to pass
5-
an *anonymous function* as a callback to another, possibly an asynchronous function.
3+
函式在 JavaScript 是第一等物件。這表示他們可以把函式當做值一樣傳遞。
4+
一個常見的用法是用 *匿名函式* 當做一個回傳去呼叫另一個函式,這是一種非同步函式
65

7-
### The `function` Declaration
6+
### 函式的宣告
87

98
function foo() {}
109

11-
The above function gets [hoisted](#function.scopes) before the execution of the
12-
program starts; thus, it is available *everywhere* in the scope it was
13-
*defined*, even if called before the actual definition in the source.
10+
上面的函式在被執行之前會被 [解析(hoisted)](#function.scopes),因此它可以在 **任意** 的地方都是 *有宣告的* ,就算是在比這個函式還早呼叫。
1411

15-
foo(); // Works because foo was created before this code runs
12+
13+
foo(); // 可以執行,因為 foo 已經在運行前就被建立
1614
function foo() {}
1715

18-
### The `function` Expression
16+
### `function` 的表達式
1917

2018
var foo = function() {};
2119

22-
This example assigns the unnamed and *anonymous* function to the variable `foo`.
20+
這個例子把一個 *匿名* 函式賦值給變數 `foo`
2321

2422
foo; // 'undefined'
25-
foo(); // this raises a TypeError
23+
foo(); // 錯誤: TypeError
2624
var foo = function() {};
2725

28-
Due to the fact that `var` is a declaration that hoists the variable name `foo`
29-
before the actual execution of the code starts, `foo` is already defined when
30-
the script gets executed.
26+
由於 `var` 已經宣告變數 `foo` 在所有的程式碼執行之前。
27+
所以 `foo`已經在程式運行前就已經被定義過了。
28+
但是因為賦值只會在運行時去職情,所以在程式碼執行前,`foo` 的值還沒被宣告所以為 [undefined](#core.undefined)
3129

32-
But since assignments only happen at runtime, the value of `foo` will default
33-
to [undefined](#core.undefined) before the corresponding code is executed.
3430

35-
### Named Function Expression
31+
### 命名函式的賦值表達式
3632

37-
Another special case is the assignment of named functions.
33+
另一個特殊狀況就勢將一個命名函式賦值給一個變數。
3834

3935
var foo = function bar() {
40-
bar(); // Works
36+
bar(); // 可以運行
4137
}
42-
bar(); // ReferenceError
38+
bar(); // 錯誤:ReferenceError
4339

44-
Here, `bar` is not available in the outer scope, since the function only gets
45-
assigned to `foo`; however, inside of `bar`, it is available. This is due to
46-
how [name resolution](#function.scopes) in JavaScript works, the name of the
47-
function is *always* made available in the local scope of the function itself.
40+
`bar` 不可以在外部的區域被執行,因為它只有在 `foo` 的函式內才可以去執行。
41+
然而在 `bar` 內部還是可以看見。這是由於 JavaScript的 [命名處理](#function.scopes)所致。
42+
函式名在函式內 ** 可以去使用。
4843

0 commit comments

Comments
 (0)