Skip to content

Commit c8a4e3d

Browse files
author
howardchi
committed
finished some other pages
1 parent 0510d99 commit c8a4e3d

File tree

7 files changed

+131
-223
lines changed

7 files changed

+131
-223
lines changed

doc/zh-TW/function/constructors.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ JavaScript 中的建構函式和其他語言中的建構函式是不同的。
44
`new` 的關鍵字方式調用的函式都被認為是建構函式。
55
在建構函式內部 - 被呼叫的函式 - `this` 指向一個新建立的 `object`[prototype](#object.prototype) 這是一個新的物件一個被指向函式的 `prototype` 的建構函式。
66

7-
If the function that was called has no explicit `return` statement, then it
8-
implicitly returns the value of `this` - the new object.
97
如果被使用的函式沒有明顯的呼叫 `return` 的表達式,它會回傳一個隱性的 `this` 的新物件。
108

119
function Foo() {

doc/zh-TW/function/scopes.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ JavaScript 中沒有寫示的命名空間定義,這代表著它所有定義的
3939
test();
4040
foo; // 21
4141

42-
Leaving out the `var` statement inside the function `test` will override the
43-
value of `foo`. While this might not seem like a big deal at first, having
44-
thousands of lines of JavaScript and not using `var` will introduce horrible,
45-
hard-to-track-down bugs.
4642
在函數 `test` 中部使用 `var` 會覆蓋到原本在外面的 `foo`
4743
雖然看起來不是什麼大問題,但是當程式有幾千行的時候沒有使用 `var` 會照成難以追蹤的臭蟲。
4844

doc/zh-TW/intro/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
## 簡介
22

3-
是一個不斷更新的文件,最主要是要去了解一些 Javascript 比較古怪的部份。
3+
JavaScript 庭院 是一個不斷更新的文件,最主要是要去了解一些 Javascript 比較古怪的部份。
44
給一些意見來防止遇到一些常見的錯誤和一些難以發現的問題,以及性能問題和不好的習慣。
5-
初學者也可以借此去了解 Javascript 這項語言的特性。
5+
初學者也可以藉此去了解 Javascript 這項語言的特性。
66

77
JavaScript 庭院 並 **不是** 要教導你 Javascript 的語言。
88
如果要能夠理解這篇文章的內容,你需要事先學習 JavaScript 的基礎知識。

doc/zh-TW/other/timeouts.md

Lines changed: 30 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,80 @@
1-
### `setTimeout` and `setInterval`
1+
### `setTimeout` `setInterval`
22

3-
Since JavaScript is asynchronous, it is possible to schedule the execution of a
4-
function using the `setTimeout` and `setInterval` functions.
3+
由於 Javascript 是一個非同步傳輸的系統,因此可以執行一個函式用 `setTimeout``setInterval`
54

6-
> **Note:** Timeouts are **not** part of the ECMAScript Standard. They are
7-
> implemented as part of the [DOM][1].
5+
> **注意:** Timeouts 不在 ECMAScript 的標準中。它們是 [DOM][1] 其中的一部分
86
97
function foo() {}
108
var id = setTimeout(foo, 1000); // returns a Number > 0
119

12-
When `setTimeout` is called, it returns the ID of the timeout and schedule
13-
`foo` to run **approximately** one thousand milliseconds in the future.
14-
`foo` will then be executed **once**.
10+
`setTimeout` 被呼叫,它會回傳一個 ID 標準並是計畫在將來 **大約** 1000 毫秒後在在去呼叫 `foo` 函式。
11+
`foo` 函式只會被執行 **一次**
1512

16-
Depending on the timer resolution of the JavaScript engine running the code, as
17-
well as the fact that JavaScript is single threaded and other code that gets
18-
executed might block the thread, it is by **no means** a safe bet that one will
19-
get the exact delay specified in the `setTimeout` call.
13+
基於 JavaScript 引擎的計時策略,以及基本的單線程運行的方式,所以其他的程式碼可以被阻塞。
14+
因此 **沒法確保**函式會在 `setTimeout` 指定的時可被調用。
2015

21-
The function that was passed as the first parameter will get called by the
22-
*global object*, which means that [`this`](#function.this) inside the called function
23-
refers to the global object.
16+
第一個參數被函式呼叫的會在 *全域物件* 被呼叫,這代表 [`this`](#function.this)在這個函式會指向全域物件。
2417

2518
function Foo() {
2619
this.value = 42;
2720
this.method = function() {
28-
// this refers to the global object
29-
console.log(this.value); // will log undefined
21+
// 指向全域
22+
console.log(this.value); // 會跑出 undefined
3023
};
3124
setTimeout(this.method, 500);
3225
}
3326
new Foo();
3427

28+
> **注意: ** `setTimeout` 第一個參數是 **參數的物件**,這是一個很常見的錯誤使用 `setTimeout(foo(), 1000),
29+
> 這裡會調用 `foo`**return value****不是** `foo` 本身。
30+
> 如果函式返回 `undefined``setTimeout` 也不會出錯。
3531
36-
> **Note:** As `setTimeout` takes a **function object** as its first parameter, an
37-
> common mistake is to use `setTimeout(foo(), 1000)`, which will use the
38-
> **return value** of the call `foo` and **not** `foo`. This is, most of the time,
39-
> a silent error, since when the function returns `undefined` `setTimeout` will
40-
> **not** raise any error.
32+
### `setInterval` 的堆調用
4133

42-
### Stacking Calls with `setInterval`
34+
`setTimeout` 只會在函式上跑一次而已, `setInterval` - 則會在每隔 `X` 毫秒執行函式一次。但不鼓勵這種寫法。
4335

44-
While `setTimeout` only runs the function once, `setInterval` - as the name
45-
suggests - will execute the function **every** `X` milliseconds, but its use is
46-
discouraged.
47-
48-
When code that is being executed blocks the timeout call, `setInterval` will
49-
still issue more calls to the specified function. This can, especially with small
50-
intervals, result in function calls stacking up.
36+
當回傳函式的執行被阻塞時, `setInterval` 仍然會發佈更多的回傳函式。在很小的定時間隔情況像會使得回傳函式被堆疊起來。
5137

5238
function foo(){
53-
// something that blocks for 1 second
39+
// 執行 1 秒
5440
}
5541
setInterval(foo, 1000);
5642

57-
In the above code, `foo` will get called once and will then block for one second.
43+
上面的程式中, `foo` 會執行一次然後被阻塞了義分鐘
5844

59-
While `foo` blocks the code, `setInterval` will still schedule further calls to
60-
it. Now, when `foo` has finished, there will already be **ten** further calls to
61-
it waiting for execution.
45+
`foo` 被阻塞的時候 `setInterval` 還是會組織將對回傳函式的調用。因此當第一次 `foo` 函式調用結束時,已經有 **10** 次函式的調用在等待執行。
6246

63-
### Dealing with Possible Blocking Code
47+
### 處理可能被阻塞的調用
6448

65-
The easiest solution, as well as most controllable solution, is to use `setTimeout` within
66-
the function itself.
49+
最簡單的解決方法,也是最容易控制的解決方法,就是在函式中使用 `setTimeout`
6750

6851
function foo(){
6952
// something that blocks for 1 second
7053
setTimeout(foo, 1000);
7154
}
7255
foo();
7356

74-
Not only does this encapsulate the `setTimeout` call, but it also prevents the
75-
stacking of calls and gives additional control. `foo` itself can now decide
76-
whether it wants to run again or not.
57+
這樣不只封裝了 `setTimeout`,也防止了堆疊的呼叫,還有給它更多的控制。 `foo` 可以去決定要不要繼續執行。
7758

78-
### Manually Clearing Timeouts
59+
### 手動清理 Timeouts
7960

80-
Clearing timeouts and intervals works by passing the respective ID to
81-
`clearTimeout` or `clearInterval`, depending on which `set` function was used
82-
in the first place.
61+
清除 timeouts 所產生的 ID 標準傳遞給 `clearTimeout``clearInterval` 函式來清除定時,
62+
至於使用哪個函式取決於調用的時候使用的是 `setTimeout` 還是 `setInterval`
8363

8464
var id = setTimeout(foo, 1000);
8565
clearTimeout(id);
8666

87-
### Clearing All Timeouts
67+
### 清除所有 Timeouts
8868

89-
As there is no built-in method for clearing all timeouts and/or intervals,
90-
it is necessary to use brute force in order to achieve this functionality.
69+
由於沒有一個內建的方法可以一次清空所有的 timeouts 和 intervals,所以只有用暴力法來達到這樣的需求。
9170

9271
// clear "all" timeouts
9372
for(var i = 1; i < 1000; i++) {
9473
clearTimeout(i);
9574
}
9675

97-
But there might still be timeouts that are unaffected by this arbitrary number.
98-
Another way of doing this is to consider that the ID given to a timeout is
99-
incremented by one every time you call `setTimeout`.
76+
可能還有一些定石器不會在上面的代碼中被清除,因此我們可以事先保存所有的定時器 ID,然後一把清除。
77+
10078

10179
// clear "all" timeouts
10280
var biggestTimeoutId = window.setTimeout(function(){}, 1),
@@ -105,12 +83,7 @@ incremented by one every time you call `setTimeout`.
10583
clearTimeout(i);
10684
}
10785

108-
Even though this works on all major browsers today, it isn't specified that
109-
the IDs should be ordered that way and it may change. Therefore, it is instead
110-
recommended to keep track of all the timeout IDs, so they can be cleared
111-
specifically.
112-
113-
### Hidden Use of `eval`
86+
### 隱藏使用 `eval`
11487

11588
`setTimeout` and `setInterval` can also take a string as their first parameter.
11689
This feature should **never** be used because it internally makes use of `eval`.

doc/zh-TW/types/casting.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ JavaScript 是一個 *弱類型* 的程式語言,所以在 **任何** 情況
1919
> **ES5 注意:** 如果數字字面值的開頭是 `0` 它會強制轉為八進位數字解析。
2020
> 而在 ES5 嚴格模式下,它已經被刪除了。
2121
22-
23-
To avoid the issues above, use of the [strict equal operator](#types.equality)
24-
is **highly** recommended. Although this avoids a lot of common pitfalls, there
25-
are still many further issues that arise from JavaScript's weak typing system.
2622
為了去避免上驗的事件發生,我們會用 [嚴格等於操作符](#types.equality) 這是強烈建議。
2723
因為它可以避免很多常見的問題,但 JavaScript 的弱類型系同仍然會導致一些其他問題。
2824

doc/zh-TW/types/typeof.md

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55

66
雖然 `instanceof` 還是有一些限制上的使用, `typeof` 只有一個實際上的運傭情形,但是 **不是** 用在檢查物件的類型。
77

8-
> **Note:** While `typeof` can also be called with a function like syntax, i.e.
9-
> `typeof(obj)`, this is not a function call. The parentheses behave as normal
10-
> and the return value will be used as the operand of the `typeof` operator.
11-
> There is **no** `typeof` function.
8+
> **注意:** 由於 `typeof` 也可以像函式的語法被調用,例如 `typeof(obj)`,但這並是一個函數調用。
9+
> 那兩個小括號只是用來計算一個表達式的值,這個返回值會作為 `typeof` 操作符的一個操作數。
10+
> 實際上 **不存在** 名為 `typeof` 的函式。
1211
1312

1413
### JavaScript 類型表格
@@ -31,22 +30,20 @@
3130
{} Object object
3231
new Object() Object object
3332

34-
In the above table, *Type* refers to the value that the `typeof` operator returns.
35-
As can be clearly seen, this value is anything but consistent.
33+
上面的表格中, *Type* 這一系列表示 `typeof` 的操作符的運算結果。可以看到,這個值的大多數情況下都返回物件。
3634

37-
The *Class* refers to the value of the internal `[[Class]]` property of an object.
35+
*Class* 表示物件內部的屬性 `[[Class]]` 的值。
3836

39-
> **From the Specification:** The value of `[[Class]]` can be one of the
40-
> following strings. `Arguments`, `Array`, `Boolean`, `Date`, `Error`,
41-
> `Function`, `JSON`, `Math`, `Number`, `Object`, `RegExp`, `String`.
4237

43-
In order to retrieve the value of `[[Class]]`, one has to make use of the
44-
`toString` method of `Object.prototype`.
38+
> **JavaScript 標準文檔中定義:** `[[Class]]`的值只可能是下面字符串中的一個:
39+
> `Arguments`, `Array`, `Boolean`, `Date`, `Error`,
40+
> `Function`, `JSON`, `Math`, `Number`, `Object`, `RegExp`, `String`
4541
46-
### The Class of an Object
42+
為了獲取對象的 `[[Class]]`,我們可以使用定義在 `Object.prototype` 上的方法 `toString`
4743

48-
The specification gives exactly one way of accessing the `[[Class]]` value,
49-
with the use of `Object.prototype.toString`.
44+
### 物件的類定義
45+
46+
JavaScript 標準文檔只給出了一種獲取 `[[Class]]` 值的方法,那就是使用 `Object.prototype.toString`
5047

5148
function is(type, obj) {
5249
var clas = Object.prototype.toString.call(obj).slice(8, -1);
@@ -56,29 +53,22 @@ with the use of `Object.prototype.toString`.
5653
is('String', 'test'); // true
5754
is('String', new String('test')); // true
5855

59-
In the above example, `Object.prototype.toString` gets called with the value of
60-
[this](#function.this) being set to the object whose `[[Class]]` value should be
61-
retrieved.
56+
上面的例子中,**`Object.prototype.toString`[this](#function.this)的值來來調用被設置需要獲取 `[[Class]]` 值的物件。
6257

63-
> **ES5 Note:** For convenience the return value of `Object.prototype.toString`
64-
> for both `null` and `undefined` was **changed** from `Object` to `Null` and
65-
> `Undefined` in ECMAScript 5.
58+
> **ES5 Note:** 為了回傳 `Object.prototyp.toString` 值的方便
59+
> `null``undefined`**改變**`object``null``undefined` 在 ECMAScript 5。
6660
67-
### Testing for Undefined Variables
61+
### 測試未定義變數
6862

6963
typeof foo !== 'undefined'
7064

71-
The above will check whether `foo` was actually declared or not; just
72-
referencing it would result in a `ReferenceError`. This is the only thing
73-
`typeof` is actually useful for.
65+
上面的例子確認 `foo` 是否真的被宣告。如果沒有定義會導致 `ReferenceError` 這是 `typeof` 唯一有用的地方
7466

75-
### In Conclusion
67+
### 結語
7668

77-
In order to check the type of an object, it is highly recommended to use
78-
`Object.prototype.toString` because this is the only reliable way of doing so.
79-
As shown in the above type table, some return values of `typeof` are not defined
80-
in the specification; thus, they can differ between implementations.
69+
為了去檢查一個物件,強烈建議去使用 `Object.prototype.toString` 因為這是唯一可以依賴的方式。
70+
正如上面所看到的 `typeof` 的亦先返回值在標準文檔中未定義,因此不同的引擎可能不同。
8171

82-
Unless checking whether a variable is defined, `typeof` should be avoided.
72+
除非為了檢測一個變數是否定義,我們應該避免是用 `typeof` 操作符。
8373

8474

0 commit comments

Comments
 (0)