Skip to content

Commit 151456f

Browse files
author
howardchi
committed
finished the part in Array & some in function section
1 parent 33a89e7 commit 151456f

27 files changed

+2072
-188
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
/site/es
1616
*.md~
1717
*.src.md
18+
*.DS_store

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

Lines changed: 0 additions & 70 deletions
This file was deleted.

doc/zh(Trandition)/function/arguments.md

Lines changed: 0 additions & 118 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

doc/zh-TW/core/undefined.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
- 進入尚未修改的全域變數 `undefined`
18+
- 進入一個宣告但 **尚未** 初始化的變數。
19+
- `return` 表示式中沒有返回任何內容。
20+
- 呼叫不存在的屬性。
21+
- 函式參數沒有被傳遞數值。
22+
- 任何被被設定為 `undefined` 的變數。
23+
- 任何表達式中形式為 `void(expression)`
24+
25+
### 處理 `undefined` 值的改變
26+
27+
由於全域變數 `undefined` 只有保存 `undefined` 類型實際值的一個副本,指定了一個新的值並 **不會** 改變 `undefined`類型裡面的值。
28+
29+
為了避免去改變 `undefined` 的值,常用的技巧就是加上一個新的變數到 [匿名包裝器](#function.scopes)。在使用的時候,這個參數不會接受任何的值。
30+
31+
var undefined = 123;
32+
(function(something, foo, undefined) {
33+
// undefined 在區域區間內得到了 `undefined` 的值
34+
35+
})('Hello World', 42);
36+
37+
另外一個可以得到同樣的效果就是在內部宣告一個變數
38+
39+
var undefined = 123;
40+
(function(something, foo) {
41+
var undefined;
42+
...
43+
44+
})('Hello World', 42);
45+
46+
唯一的不同就是在下者會多 4 個多 bytes 用來壓縮檔案,而且函數內野沒有其他需要使用 `var`
47+
48+
### 使用 `null`
49+
50+
JavaScript 中所使用的 `undefined` 類似別的語言中的 *null* , 但實際上在 JavaScript 中的 `null` 算是另外一個類型。
51+
52+
它在 JavaScript 有些可以使用的地方 (例如說宣告一個原型的終結,例如 `Foo.prototype = null` )。
53+
但是在大部分的時候可以用 `undefined`,來取代。
54+
55+

doc/zh-TW/function/arguments.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
## `arguments` 物件
2+
3+
所有函數在 JavaScript 中都可以有個特別的參數 `arguments`
4+
這個變數掌握了一列傳入函數中的參數
5+
6+
> **注意:** 由於 `arguments` 都已經在函數中被定義了
7+
> 經過 `var` 定義或是用 `arguments` 宣告參數
8+
> `arguments` 物件都不會被建立
9+
10+
`arguments` 物件 **不是** 一個 `Array`,雖然都有很多 Array 的語法 - 就像是 `length` 屬性 - 但是它沒有繼承來自 `Array.prototype` 事實上它繼承 `object`
11+
12+
由於這些原因,這 **不可能** 用 Array 的一些功能像是 `push``pop`或是 `slice``arguments`
13+
但是像 `for` 迴圈這些迴圈都是可以用的,如果真的需要使用一些標準的 `Array` 功能可以先把它轉成真的 `Array` 再去使用。
14+
15+
### 轉為 Array
16+
17+
下面的程式可以回傳一個新的 `Array` 包含所有的元素在 `Arguments`的物件中
18+
19+
Array.prototype.slice.call(arguments);
20+
21+
這種轉化方式比較 **** ,不建議使用這種作法如果再追求效率的程式中。
22+
23+
24+
### 傳遞參數
25+
26+
下面是建議用這種方式去傳參數到另一個函數
27+
28+
function foo() {
29+
bar.apply(null, arguments);
30+
}
31+
function bar(a, b, c) {
32+
// 在這裡做一些事情
33+
}
34+
35+
另一個技巧是用 `call``apply` 放在一起來創造一個更快的解綁定包裝器
36+
37+
function Foo() {}
38+
39+
Foo.prototype.method = function(a, b, c) {
40+
console.log(this, a, b, c);
41+
};
42+
43+
// Create an unbound version of "method"
44+
// 輸入的參數: this, arg1, arg2...argN
45+
Foo.method = function() {
46+
47+
// 結果: Foo.prototype.method.call(this, arg1, arg2... argN)
48+
Function.call.apply(Foo.prototype.method, arguments);
49+
};
50+
51+
52+
### 自動更新
53+
54+
`Arguments` 物件創造的 *getter**setter* 的函數方法,可以被視為原本函數的變數。
55+
56+
因此,改變了一個變數會跟著改變它的值而且也間接的改變稻香對應的 `arguments` 的物件,反之亦然。
57+
58+
function foo(a, b, c) {
59+
arguments[0] = 2;
60+
a; // 2
61+
62+
b = 4;
63+
arguments[1]; // 4
64+
65+
var d = c;
66+
d = 9;
67+
c; // 3
68+
}
69+
foo(1, 2, 3);
70+
71+
### 性能
72+
73+
`arguments` 總是會被宣告,但除了兩個情況,一個是在一個函式中或是在其中一個參入。而不論他是否有被使用。
74+
75+
*getters**setter* 會永遠被創造。然而,他們對任何性能都沒有影響,除非對它的屬性有多次的訪問
76+
77+
78+
> **ES5 提示:** 那些 *getters**setters* 在嚴格的模式像不會被建立
79+
80+
然而會有一種情況來降低 JavaScript 引擎的效能。就是使用 `arguments.callee`
81+
82+
function foo() {
83+
arguments.callee; // 做一些在這個函數物件
84+
arguments.callee.caller; // 然後呼叫這個函數物件
85+
}
86+
87+
function bigLoop() {
88+
for(var i = 0; i < 100000; i++) {
89+
foo(); // 通常會在內聯
90+
}
91+
}
92+
93+
在上面的程式中, `foo` 不再是一個單存的互聯函數
94+
因為它需要知道他自己和它的調用者。
95+
這不僅減低了它的性能,而且還破壞的封裝
96+
97+
**強烈建議不要使用** `arguments.callee` 或是其他它的屬性
98+
99+
> **ES5 Note:** 在嚴格的模式下 `arguments.callee` 會丟出一個 `TypeError`, 因為這種方法已經被廢除了
100+
101+
[1]: http://en.wikipedia.org/wiki/Inlining
102+
103+
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)