Skip to content

Commit 5804c90

Browse files
author
howardchi
committed
modify language.json, index.json, constructors.md
1 parent 4a26176 commit 5804c90

File tree

5 files changed

+67
-96
lines changed

5 files changed

+67
-96
lines changed

doc/language.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"default": "en",
3-
"listed": ["en", "fi", "ru", "zh", "tr", "pl", "ko", "ja", "es"]
3+
"listed": ["en", "fi", "ru", "zh", "tr", "pl", "ko", "ja", "es", "zh-Tw"]
44
}
55

doc/zh-TW/function/constructors.md

Lines changed: 28 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
## Constructors
1+
## 建構函式
22

3-
Constructors in JavaScript are yet again different from many other languages. Any
4-
function call that is preceded by the `new` keyword acts as a constructor.
5-
6-
Inside the constructor - the called function - the value of `this` refers to a
7-
newly created object. The [prototype](#object.prototype) of this **new**
8-
object is set to the `prototype` of the function object that was invoked as the
9-
constructor.
3+
JavaScript 中的建構函式和其他語言中的建構函式是不同的。
4+
`new` 的關鍵字方式調用的函式都被認為是建構函式。
5+
在建構函式內部 - 被呼叫的函式 - `this` 指向一個新建立的 `object`[prototype](#object.prototype) 這是一個新的物件一個被指向函式的 `prototype` 的建構函式。
106

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

1411
function Foo() {
1512
this.bla = 1;
@@ -21,16 +18,13 @@ implicitly returns the value of `this` - the new object.
2118

2219
var test = new Foo();
2320

24-
The above calls `Foo` as constructor and sets the `prototype` of the newly
25-
created object to `Foo.prototype`.
26-
27-
In case of an explicit `return` statement, the function returns the value
28-
specified by that statement, but **only** if the return value is an `Object`.
21+
在上面的例子中 `Foo` 建立一個建構函式,並設立一個 `prototype` 來創建一個新的物件叫 `Foo.prototype`
22+
這個情況下它顯示的 `return` 一個表達式,但他 **** 返回一個 `Object`
2923

3024
function Bar() {
3125
return 2;
3226
}
33-
new Bar(); // a new object
27+
new Bar(); // 返回一個新物件
3428

3529
function Test() {
3630
this.value = 2;
@@ -39,23 +33,20 @@ specified by that statement, but **only** if the return value is an `Object`.
3933
foo: 1
4034
};
4135
}
42-
new Test(); // the returned object
36+
new Test(); // 回傳物件
4337

44-
When the `new` keyword is omitted, the function will **not** return a new object.
38+
如果 `new` 的關鍵字被忽略,函式就 **不會** 回傳一個新的物件。
4539

4640
function Foo() {
47-
this.bla = 1; // gets set on the global object
41+
this.bla = 1; // 獲取一個全域的參數
4842
}
4943
Foo(); // undefined
5044

51-
While the above example might still appear to work in some cases, due to the
52-
workings of [`this`](#function.this) in JavaScript, it will use the
53-
*global object* as the value of `this`.
45+
雖然上面有些情況也能正常運行,但是由於 JavaScript 中 [`this`](#funciton.this) 的工作原理,這裡的 `this` 指向 *全域對象*
5446

55-
### Factories
47+
### 工廠模式
5648

57-
In order to be able to omit the `new` keyword, the constructor function has to
58-
explicitly return a value.
49+
為了不使用 `new` 關鍵字,建構函式必須顯性的返回一個值。
5950

6051
function Bar() {
6152
var value = 1;
@@ -72,25 +63,17 @@ explicitly return a value.
7263
new Bar();
7364
Bar();
7465

75-
Both calls to `Bar` return the same thing, a newly create object that
76-
has a property called `method`, which is a
77-
[Closure](#function.closures).
78-
79-
It should also be noted that the call `new Bar()` does **not** affect the
80-
prototype of the returned object. While the prototype will be set on the newly
81-
created object, `Bar` never returns that new object.
82-
83-
In the above example, there is no functional difference between using and
84-
not using the `new` keyword.
66+
上面兩個呼叫 `Bar` 的方法回傳的值都一樣,一個新創建的擁有 `method` 屬性被返回,這裡創建了一個 [Closure](#function.closures).
8567

68+
還有注意, `new Bar()`**不會** 改變返回物件的原型。
69+
因為建構函式的原型會指向剛剛創立的新物件,而在這裡的 `Bar` 沒有把這個新物件返回。
70+
在上面的例子中,使用或者不使用 `new` 關鍵字沒有什麼功能性的區別
8671

87-
### Creating New Objects via Factories
8872

89-
It is often recommended to **not** use `new` because forgetting its use may
90-
lead to bugs.
73+
### 通過工廠模式創建的新對象
9174

92-
In order to create a new object, one should rather use a factory and construct a
93-
new object inside of that factory.
75+
常聽到建議 **不要** 使用 `new`,因為如果忘記如何使用它會造成錯誤。
76+
為了創建一個新的物件,我們可以用工廠方法,來創造一個新的物件在那個方法中。
9477

9578
function Foo() {
9679
var obj = {};
@@ -107,22 +90,14 @@ new object inside of that factory.
10790
return obj;
10891
}
10992

110-
While the above is robust against a missing `new` keyword and certainly makes
111-
the use of [private variables](#function.closures) easier, it comes with some
112-
downsides.
93+
雖然上面的方式比起 `new` 的調用方式更不容易出錯,並且可以充分的使用 [私有變數](#function.closures)所帶來的便利,但是還是有一些不好的地方
11394

114-
1. It uses more memory since the created objects do **not** share the methods
115-
on a prototype.
116-
2. In order to inherit, the factory needs to copy all the methods from another
117-
object or put that object on the prototype of the new object.
118-
3. Dropping the prototype chain just because of a left out `new` keyword
119-
is contrary to the spirit of the language.
12095

121-
### In Conclusion
96+
1. 會占用更多的記憶體,因為創建的物件 **沒有** 辦法放在在同一個原型上。
97+
2. 為了要用繼承的方式,工廠方法需要複製所有的屬性或是把一個物件作為新的物件的原型。
98+
3. 放棄原型鏈僅僅是因為防止遺漏 `new` 所帶來的問題,這與語言本身的思想鄉違背。
12299

123-
While omitting the `new` keyword might lead to bugs, it is certainly **not** a
124-
reason to drop the use of prototypes altogether. In the end it comes down to
125-
which solution is better suited for the needs of the application. It is
126-
especially important to choose a specific style of object creation and use it
127-
**consistently**.
100+
### 結語
128101

102+
雖然遺漏 `new` 關鍵字可能會導致問題,但這並 **不是** 放棄只用原型的藉口。
103+
最終使用哪種方式取決於應用程式的需求,選擇一種程式語言風格並堅持下去才是最重要的。

doc/zh-TW/function/scopes.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
## Scopes and Namespaces
1+
## 作用域和命名空間
22

3-
Although JavaScript deals fine with the syntax of two matching curly
4-
braces for blocks, it does **not** support block scope; hence, all that is left
5-
in the language is *function scope*.
3+
儘管 JavaScript 支持一個大括號創建的程式碼,但並不支持塊級作用域。
4+
而僅僅支援 *函式作用域*
65

7-
function test() { // a scope
8-
for(var i = 0; i < 10; i++) { // not a scope
9-
// count
6+
function test() { // 一個作用域
7+
for(var i = 0; i < 10; i++) { // 不是一個作用域
8+
// 算數
109
}
1110
console.log(i); // 10
1211
}
@@ -16,14 +15,15 @@ in the language is *function scope*.
1615
> **not** as an object literal. This, in conjunction with
1716
> [automatic insertion of semicolons](#core.semicolon), can lead to subtle errors.
1817
18+
1919
There are also no distinct namespaces in JavaScript, which means that everything
2020
gets defined in one *globally shared* namespace.
2121

2222
Each time a variable is referenced, JavaScript will traverse upwards through all
2323
the scopes until it finds it. In the case that it reaches the global scope and
2424
still has not found the requested name, it will raise a `ReferenceError`.
2525

26-
### The Bane of Global Variables
26+
### 全域變數的壞處
2727

2828
// script A
2929
foo = '42';
@@ -222,7 +222,7 @@ which, while different in syntax, behave the same way.
222222
(function(){}());
223223
// and so on...
224224

225-
### In Conclusion
225+
### 結語
226226

227227
It is recommended to always use an *anonymous wrapper* to encapsulate code in
228228
its own namespace. This does not only protect code against name clashes, but it

doc/zh-TW/function/this.md

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,46 @@
1-
## How `this` Works
1+
## `this` 的工作原理
22

3-
JavaScript has a different concept of what the special name `this` refers to
4-
than most other programming languages. There are exactly **five** different
5-
ways in which the value of `this` can be bound in the language.
3+
JavaScript 有移到完全部屬於其他語言處理 `this` 的處理機制。
4+
**** 種物同的情況下, `this` 指向的個不相同
65

7-
### The Global Scope
6+
### 全域變數
87

98
this;
109

11-
When using `this` in global scope, it will simply refer to the *global* object.
10+
如果再全域範圍內使用 `this`,會指向 *全域* 的物件
1211

1312

14-
### Calling a Function
13+
### 呼叫一個函式
1514

1615
foo();
1716

18-
Here, `this` will again refer to the *global* object.
17+
這裡 `this` 也會指向 *全域* 對象。
1918

20-
> **ES5 Note:** In strict mode, the global case **no longer** exists.
21-
> `this` will instead have the value of `undefined` in that case.
19+
> **ES5 注意:** 在嚴格模式下,不存在全域變數。
20+
> `this` 將會是 `undefined`
2221
23-
### Calling a Method
22+
### 方法調用
2423

2524
test.foo();
2625

27-
In this example, `this` will refer to `test`.
26+
這個例子中, `this` 指向 `test` 物件。
2827

29-
### Calling a Constructor
28+
### 呼叫一個建構函式
3029

3130
new foo();
3231

33-
A function call that is preceded by the `new` keyword acts as
34-
a [constructor](#function.constructors). Inside the function, `this` will refer
35-
to a *newly created* `Object`.
32+
如果函式傾向用 `new` 關鍵詞使用,我們稱這個函式為 [建構函式](#function.constructors)
33+
在函式內部, `this` 指向 *新物件的創立*
3634

37-
### Explicit Setting of `this`
35+
### 顯示的設置 `this`
3836

3937
function foo(a, b, c) {}
4038
4139
var bar = {};
42-
foo.apply(bar, [1, 2, 3]); // array will expand to the below
43-
foo.call(bar, 1, 2, 3); // results in a = 1, b = 2, c = 3
40+
foo.apply(bar, [1, 2, 3]); // Array 會被擴展,如下所示
41+
foo.call(bar, 1, 2, 3); // 傳遞參數 a = 1, b = 2, c = 3
4442

45-
When using the `call` or `apply` methods of `Function.prototype`, the value of
46-
`this` inside the called function gets **explicitly set** to the first argument
47-
of the corresponding function call.
43+
當使用 `function.prototype` 上的 `call` 或只 `apply` 方法時,函式內的 `this` 將會被 **顯示設置** 為函式調用的第一個參數。
4844

4945
As a result, in the above example the *method case* does **not** apply, and `this`
5046
inside of `foo` will be set to `bar`.
@@ -53,7 +49,7 @@ inside of `foo` will be set to `bar`.
5349
> literal. So `var obj = {me: this}` will **not** result in `me` referring to
5450
> `obj`, since `this` only gets bound by one of the five listed cases.
5551
56-
### Common Pitfalls
52+
### 常見誤解
5753

5854
While most of these cases make sense, the first can be considered another
5955
mis-design of the language because it **never** has any practical use.

doc/zh-TW/index.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
2-
"title": "JavaScript Garden",
3-
"langTitle": "JavaScript Garden in English",
4-
"description": "A Guide to JavaScript's Quirks and Flaws.",
2+
"title": "JavaScript 庭院",
3+
"langTitle": "JavaScript Garden 繁體中文翻譯",
4+
"description": "JavaScript 語言中古怪用法及缺點的文件總集",
55
"sections": [
66
{
7-
"title": "Intro",
7+
"title": "簡介",
88
"dir": "intro",
99
"articles": []
1010
},
1111
{
12-
"title": "Objects",
12+
"title": "物件",
1313
"dir": "object",
1414
"articles": [
1515
"general",
@@ -19,7 +19,7 @@
1919
]
2020
},
2121
{
22-
"title": "Functions",
22+
"title": "函式",
2323
"dir": "function",
2424
"articles": [
2525
"general",
@@ -31,15 +31,15 @@
3131
]
3232
},
3333
{
34-
"title": "Arrays",
34+
"title": "陣列",
3535
"dir": "array",
3636
"articles": [
3737
"general",
3838
"constructor"
3939
]
4040
},
4141
{
42-
"title": "Types",
42+
"title": "類型",
4343
"dir": "types",
4444
"articles": [
4545
"equality",
@@ -49,7 +49,7 @@
4949
]
5050
},
5151
{
52-
"title": "Core",
52+
"title": "核心",
5353
"dir": "core",
5454
"articles": [
5555
"eval",
@@ -59,7 +59,7 @@
5959
]
6060
},
6161
{
62-
"title": "Other",
62+
"title": "其他",
6363
"dir": "other",
6464
"articles": [
6565
"timeouts"

0 commit comments

Comments
 (0)