Skip to content

Commit 33a89e7

Browse files
author
howardchi
committed
translate to Tranditioin Chinese
1 parent a2d2b63 commit 33a89e7

23 files changed

+1983
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## `Array` 的建構函式
2+
3+
`Array` 的建構函式在處理參數上一直有模糊的地帶,所以建議使用 `array`的字面語法來使用 - `[]` - 來新增一個的Array
4+
5+
[1, 2, 3]; // 結果: [1, 2, 3]
6+
new Array(1, 2, 3); // 結果: [1, 2, 3]
7+
8+
[3]; // 結果: [3]
9+
new Array(3); // 結果: []
10+
new Array('3') // 結果: ['3']
11+
12+
在上面的範例 `new Array(3)` 當只有一個參數傳入到 `Array` 的建構函數
13+
且那個參數事宜個數字,建構函數會回傳空值
14+
但是 `Array` 長度的屬性會變成跟那個參數一樣(以此範例來看他回傳的長度為 3)
15+
**注意** 只有他長度的屬性會被設定,整個 Array裡面的數值都不會初始化
16+
17+
var arr = new Array(3);
18+
arr[1]; // undefined
19+
1 in arr; // false, 數值沒有被設定進去
20+
21+
被設定用來當做 `Array` 的長度只有少數情況使用
22+
先設定 `Array` 的長度可以用一下的範例來避免使用 `for loop` 的麻煩
23+
24+
new Array(count + 1).join(stringToRepeat);
25+
26+
### 結語
27+
28+
`Array` 的建構函式需要避免,建議使用字面語法。因為他們比較簡短、也更增加閱讀性

doc/zh(Trandition)/array/general.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## Array 迴圈和屬性
2+
3+
雖然在 Javascript 中 Array 都是 Objects,但是沒有好的理由要使用他
4+
[`for in`](#object.forinloop) 的迴圈中。事實上有很多原因要避免使用 `for in` 在 Array 之中
5+
6+
> **注意:** Javascript Arrays **不是** *關連性 Arrays*
7+
> 只有 [objects](#object.general) 來管理建值的相對應關係
8+
> Arrays 是**保持** 順序的,Objects **則沒有**
9+
10+
因為 `for in` 迴圈會列舉所有在原型 Array 上的屬性因為他會使用[`hasOwnProperty`](#object.hasownproperty), 這會使得 Array 比原本的 `for` 迴圈慢上二十幾倍
11+
12+
### 迴圈
13+
14+
為了要達到最好的性能所以最好使用 `for` 迴圈來讀取一個 Array 裡面的數值。
15+
16+
var list = [1, 2, 3, 4, 5, ...... 100000000];
17+
for(var i = 0, l = list.length; i < l; i++) {
18+
console.log(list[i]);
19+
}
20+
21+
在上面的例子中利用 `l = list.length` 來處理 Array 的長度問題。
22+
23+
雖然 `length` 屬性是屬於 Array 中其中一個屬性,但是他還使有一定的性能消耗在每次循環的訪問。
24+
近期 Javascript 使用 **may** 來解決在這上面的效率問題,但是在現在的引擎上還不一定有支援。
25+
26+
實際上,不使用暫存 Array 長度的方式比使用暫存的版本還要慢很多。
27+
28+
### `length` 的屬性
29+
30+
`length` 屬性中的 *getter* 直接回傳在 Array 之中的程度,而 *setter* 可以用來 **刪除** Array。
31+
32+
var foo = [1, 2, 3, 4, 5, 6];
33+
foo.length = 3;
34+
foo; // [1, 2, 3]
35+
36+
foo.length = 6;
37+
foo.push(4);
38+
foo; // [1, 2, 3, undefined, undefined, undefined, 4]
39+
40+
在上面的例子可以看到,如果給的長度比較小他就會去刪除 Array 中的數值。如果比較大的話,他就會自己增加一些 `undefined` 的數值進去
41+
42+
### 結語
43+
44+
為了達到更好的效率,建議使用 `for` 迴圈還有暫存 `length` 的屬性。
45+
`for in` 迴圈則是會讓程式中有更多的錯誤和性能問題。
46+

doc/zh(Trandition)/core/delete.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
## The `delete` Operator
2+
3+
In short, it's *impossible* to delete global variables, functions and some other
4+
stuff in JavaScript which have a `DontDelete` attribute set.
5+
6+
### Global code and Function code
7+
8+
When a variable or a function is defined in a global or a [function
9+
scope](#function.scopes) it is a property of either the Activation object or
10+
the Global object. Such properties have a set of attributes, one of which is
11+
`DontDelete`. Variable and function declarations in global and function code
12+
always create properties with `DontDelete`, and therefore cannot be deleted.
13+
14+
// global variable:
15+
var a = 1; // DontDelete is set
16+
delete a; // false
17+
a; // 1
18+
19+
// normal function:
20+
function f() {} // DontDelete is set
21+
delete f; // false
22+
typeof f; // "function"
23+
24+
// reassigning doesn't help:
25+
f = 1;
26+
delete f; // false
27+
f; // 1
28+
29+
### Explicit properties
30+
31+
Explicitly set properties can be deleted normally.
32+
33+
// explicitly set property:
34+
var obj = {x: 1};
35+
obj.y = 2;
36+
delete obj.x; // true
37+
delete obj.y; // true
38+
obj.x; // undefined
39+
obj.y; // undefined
40+
41+
In the example above, `obj.x` and `obj.y` can be deleted because they have no
42+
`DontDelete` atribute. That's why the example below works too.
43+
44+
// this works fine, except for IE:
45+
var GLOBAL_OBJECT = this;
46+
GLOBAL_OBJECT.a = 1;
47+
a === GLOBAL_OBJECT.a; // true - just a global var
48+
delete GLOBAL_OBJECT.a; // true
49+
GLOBAL_OBJECT.a; // undefined
50+
51+
Here we use a trick to delete `a`. [`this`](#function.this) here refers
52+
to the Global object and we explicitly declare variable `a` as its property
53+
which allows us to delete it.
54+
55+
IE (at least 6-8) has some bugs, so the code above doesn't work.
56+
57+
### Function arguments and built-ins
58+
59+
Functions' normal arguments, [`arguments` objects](#function.arguments)
60+
and built-in properties also have `DontDelete` set.
61+
62+
// function arguments and properties:
63+
(function (x) {
64+
65+
delete arguments; // false
66+
typeof arguments; // "object"
67+
68+
delete x; // false
69+
x; // 1
70+
71+
function f(){}
72+
delete f.length; // false
73+
typeof f.length; // "number"
74+
75+
})(1);
76+
77+
### Host objects
78+
79+
The behaviour of `delete` operator can be unpredictable for hosted objects. Due
80+
to the specification, host objects are allowed to implement any kind of behavior.
81+
82+
### In conclusion
83+
84+
The `delete` operator often has unexpected behaviour and can only be safely
85+
used to delete explicitly set properties on normal objects.

doc/zh(Trandition)/core/eval.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## 為什麼不要使用 `eval`
2+
3+
因為 `eval` 函數會在 Javascript 的區域性的區間執行那段程式碼。
4+
5+
var foo = 1;
6+
function test() {
7+
var foo = 2;
8+
eval('foo = 3');
9+
return foo;
10+
}
11+
test(); // 3
12+
foo; // 1
13+
14+
但是, `eval` 只接受直接的呼叫而且那個函數只能叫做 `eval`,才能在一個區段中執行。
15+
16+
var foo = 1;
17+
function test() {
18+
var foo = 2;
19+
var bar = eval;
20+
bar('foo = 3');
21+
return foo;
22+
}
23+
test(); // 2
24+
foo; // 3
25+
26+
所有的 `eval` 都應該去比免試用。有 99.9% 的使用情況都可以 **不必** 使用到而達到同等效果。
27+
28+
### 偽裝的 `eval`
29+
30+
[定時函數](#other.timeouts) `setTimeout``setInterval` 都可以接受一個字串當做他們第一個參數。這些字串 **永遠** 都會在全域範圍內執行,因此在這種情況下 `eval` 沒有被直接的使用。
31+
32+
### 安全上的顧慮
33+
34+
`eval` 同樣有安全上的問題,因為所有的程式碼都可以被直接執行。
35+
而他不應去執行一串未知的字串或是來自不幸任的來源。
36+
37+
### 結語
38+
39+
`eval` 應該永遠不要去只用它,任何的程式在被他執行後都有性能和安全上的考慮。如果有情況需要去使用他,他都不應該列為第一順位的解決方法。
40+
41+
應該有更好的方法能夠去使用,但是最好都不要去使用 `eval`
42+

doc/zh(Trandition)/core/semicolon.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
## 自動插入分號
2+
3+
雖然 JavaScript 有 C 語言的語法,但是他不強制一定要加上分號。
4+
所以分號可以被忽略。
5+
6+
Javascript 並 **不是** 一個不需要分號的語言。實際上,它需要分號來讓程式碼更容易被理解。因此 Javascript 的編譯器中遇到了缺少分號的情形,它會自動的在程式碼中插入分號。
7+
8+
var foo = function() {
9+
} // 編輯錯誤,因沒分號
10+
test()
11+
12+
這時候編譯器在編輯的時候,會自動的加上分號,然後重新編輯。
13+
14+
var foo = function() {
15+
}; // 沒有錯誤,編輯繼續
16+
test()
17+
18+
自動的加入分號是被認為 **最大** 的設計缺陷之一,因為它能改變程式碼的行為。
19+
20+
### 工作原理
21+
22+
下面的程式碼中沒有使用任何的分號,所以編譯器需要去決定在哪些地方加入分號。
23+
24+
(function(window, undefined) {
25+
function test(options) {
26+
log('testing!')
27+
28+
(options.list || []).forEach(function(i) {
29+
30+
})
31+
32+
options.value.test(
33+
'long string to pass here',
34+
'and another long string to pass'
35+
)
36+
37+
return
38+
{
39+
foo: function() {}
40+
}
41+
}
42+
window.test = test
43+
44+
})(window)
45+
46+
(function(window) {
47+
window.someLibrary = {}
48+
49+
})(window)
50+
51+
下面的程式碼是編譯器 **猜測** 的結果。
52+
53+
(function(window, undefined) {
54+
function test(options) {
55+
56+
// 沒有加入分號,兩行被合併為一行
57+
log('testing!')(options.list || []).forEach(function(i) {
58+
59+
}); // <- 插入分號
60+
61+
options.value.test(
62+
'long string to pass here',
63+
'and another long string to pass'
64+
); // <- 插入分號
65+
66+
return; // <- 插入分號,改變了 return 的表達行為
67+
{ // 作為另一個程式碼的處理
68+
69+
// 被當做一個獨立的函數來看
70+
foo: function() {}
71+
}; // <- 插入分號
72+
}
73+
window.test = test; // <- 插入分號
74+
75+
// 兩行又被合併
76+
})(window)(function(window) {
77+
window.someLibrary = {}; // <- 插入分號
78+
79+
})(window); //<- 插入分號
80+
81+
> **注意:** 在這個範例中 Javascript 編譯器沒有正確的處理 `return` ,因為緊接的換行符號。
82+
> 雖然這不能算是自動分號插入的錯誤,但是它是非常不樂見的效果。
83+
84+
編譯器在上面的程式碼中改變了原本程式碼的行為。在一些情況下,會做出 **錯誤的行為**
85+
86+
### 前置括號
87+
88+
在這種前置括號的情況下,編譯器 **不會** 自動的插入分號。
89+
90+
log('testing!')
91+
(options.list || []).forEach(function(i) {})
92+
93+
上面的程式碼被編譯器轉為只有一行程式
94+
95+
log('testing!')(options.list || []).forEach(function(i) {})
96+
97+
以上的範例中 `log`**很大** 的可能 **不是** 回傳一個函數。然而這個情況下會出現 `TypeError` 的錯誤或是會出現 `undefined is not a function` .
98+
99+
### 結語
100+
101+
建議永遠 **不要** 忽略分號。同樣的也建議大括號應在他對應的表達式在同一行。在 `if... else...`的表達式中也是如此,不應省略大括號。
102+
這個習慣可以不僅僅是讓你的程式更一致,也可以避免編譯器因為改變程式而出錯。
103+

doc/zh(Trandition)/core/undefined.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
## `undefined``null`
2+
3+
JavaScript 中有兩個表示空值的方式, `null``undefined``undefined`式比較常用的一種。
4+
5+
### `undefined` 的值
6+
7+
`undefined` 是一個值為 `undefined` 的類型。
8+
9+
語言中也定義了一個全域變數,它的值為 `undefined`,這個變數的被稱作 `undefined`
10+
這個變數 **不是** 一個常數,也不是一個關鍵字。這表示它的值可以被輕易的覆蓋。
11+
12+
> **ES5 提示: ** `undefined` 在 ECMAScript 5 裡 **不再是** *可寫*
13+
> 但是它的名稱還是可以被隱藏,比如說定義一個函數為 `undefined`
14+
15+
這裡有一些例子會回傳 `undefined` 的值:
16+
17+
- Accessing the (unmodified) global variable `undefined`.
18+
- Accessing a declared *but not* yet initialized variable.
19+
- Implicit returns of functions due to missing `return` statements.
20+
- `return` statements that do not explicitly return anything.
21+
- Lookups of non-existent properties.
22+
- Function parameters that do not have any explicit value passed.
23+
- Anything that has been set to the value of `undefined`.
24+
- Any expression in the form of `void(expression)`
25+
26+
### Handling Changes to the Value of `undefined`
27+
28+
Since the global variable `undefined` only holds a copy of the actual *value* of
29+
`undefined`, assigning a new value to it does **not** change the value of the
30+
*type* `undefined`.
31+
32+
Still, in order to compare something against the value of `undefined`, it is
33+
necessary to retrieve the value of `undefined` first.
34+
35+
To protect code against a possible overwritten `undefined` variable, a common
36+
technique used is to add an additional parameter to an [anonymous
37+
wrapper](#function.scopes) that gets no argument passed to it.
38+
39+
var undefined = 123;
40+
(function(something, foo, undefined) {
41+
// undefined in the local scope does
42+
// now again refer to the value `undefined`
43+
44+
})('Hello World', 42);
45+
46+
Another way to achieve the same effect would be to use a declaration inside the
47+
wrapper.
48+
49+
var undefined = 123;
50+
(function(something, foo) {
51+
var undefined;
52+
...
53+
54+
})('Hello World', 42);
55+
56+
The only difference here is that this version results in 4 more bytes being
57+
used in case it is minified, and there is no other `var` statement inside the
58+
anonymous wrapper.
59+
60+
### Uses of `null`
61+
62+
While `undefined` in the context of the JavaScript language is mostly used in
63+
the sense of a traditional *null*, the actual `null` (both a literal and a type)
64+
is more or less just another data type.
65+
66+
It is used in some JavaScript internals (like declaring the end of the
67+
prototype chain by setting `Foo.prototype = null`), but in almost all cases, it
68+
can be replaced by `undefined`.
69+
70+

0 commit comments

Comments
 (0)