11## ` this ` 的工作原理
22
3- JavaScript 有移到完全部屬於其他語言處理 ` this ` 的處理機制。
3+ JavaScript 有一道完全不屬於其他語言處理 ` this ` 的處理機制。
44在 ** 五** 種不同的情況下, ` this ` 指向的各不相同
55
66### 全域變數
@@ -42,65 +42,56 @@ JavaScript 有移到完全部屬於其他語言處理 `this` 的處理機制。
4242
4343當使用 ` function.prototype ` 上的 ` call ` 或只 ` apply ` 方法時,函式內的 ` this ` 將會被 ** 顯示設置** 為函式調用的第一個參數。
4444
45- As a result, in the above example the * method case* does ** not** apply, and ` this `
46- inside of ` foo ` will be set to ` bar ` .
45+ 因此,在以上的例子中已不適用* 函式調用* 的原則,而且` this ` 會被設定指向` bar ` 。
4746
4847> ** Note:** ` this ` ** cannot** be used to refer to the object inside of an ` Object `
4948> literal. So ` var obj = {me: this} ` will ** not** result in ` me ` referring to
5049> ` obj ` , since ` this ` only gets bound by one of the five listed cases.
5150
5251### 常見誤解
5352
54- While most of these cases make sense, the first can be considered another
55- mis-design of the language because it ** never** has any practical use.
53+ 儘管大部分的例子都合理,但第一個例子(譯者注: 應該是指前面呼叫一個函式的那個例子)可以被視為一個語言的不良設計,因為它** 從來** 就沒有實際用途。
5654
5755 Foo.method = function() {
5856 function test() {
59- // this is set to the global object
57+ // this 設定為全域
6058 }
6159 test();
6260 };
6361
64- A common misconception is that ` this ` inside of ` test ` refers to ` Foo ` ; while in
65- fact, it ** does not** .
62+ 一個常見的誤解是 ` test ` 中的 ` this ` 指向 ` Foo ` 物件,但實際上並** 不是** 。
6663
67- In order to gain access to ` Foo ` from within ` test ` , it is necessary to create a
68- local variable inside of ` method ` that refers to ` Foo ` .
64+ 為了在 ` test ` 中使用 ` Foo ` 物件,我們需要在 ` method ` 函式内部建立一個區域變數指向 ` Foo ` 。
6965
7066 Foo.method = function() {
7167 var that = this;
7268 function test() {
73- // Use that instead of this here
69+ // 這裡使用 that 而非 this
7470 }
7571 test();
7672 };
7773
78- ` that ` is just a normal variable name, but it is commonly used for the reference to an
79- outer ` this ` . In combination with [ closures] ( #function.closures ) , it can also
80- be used to pass ` this ` values around.
74+ ` that ` 只是普通的名字,不過這個名字常被用用來指向外部的 ` this ` 。 在 [ 閉包] ( #function.closures ) 一節,可以看到它(` that ` )可以取代 ` this ` 傳遞。
8175
82- As of ECMAScript 5 you can use the ` bind ` method combined with an anonymous function to achieve the same result.
76+ 在 ECMAScript 5 ,你可以使用 ` bind ` 結合匿名函式達到相同結果。
8377
8478 Foo.method = function() {
8579 var test = function() {
86- // this now refers to Foo
80+ // this 指向 Foo
8781 }.bind(this);
8882 test();
8983 };
9084
91- ### Assigning Methods
85+ ### 函式表達式
9286
93- Another thing that does ** not** work in JavaScript is function aliasing, which is
94- ** assigning** a method to a variable.
87+ 另一個在 JavaScript 中** 不會** 運作的就是 function aliasing,也就是函式** 賦值** 給一個變數。
9588
9689 var test = someObject.methodTest;
9790 test();
9891
99- Due to the first case, ` test ` now acts like a plain function call; therefore,
100- ` this ` inside it will no longer refer to ` someObject ` .
92+ 上例中,` test ` 就像一個普通的函式被调用;因此,函式内的 this 將不再指向 ` someObject ` 。
10193
102- While the late binding of ` this ` might seem like a bad idea at first, in
103- fact, it is what makes [ prototypal inheritance] ( #object.prototype ) work.
94+ 雖然起初 ` this ` 的绑定特性似乎像是個壞主意,但事實上,它使得 [ 原型繼承] ( #object.prototype ) 得以運作。
10495
10596 function Foo() {}
10697 Foo.prototype.method = function() {};
@@ -110,7 +101,5 @@ fact, it is what makes [prototypal inheritance](#object.prototype) work.
110101
111102 new Bar().method();
112103
113- When ` method ` gets called on an instance of ` Bar ` , ` this ` will now refer to that
114- very instance.
115-
104+ 當 ` method ` 被呼叫時,` this ` 將會指向 ` Bar ` 的實體物件。
116105
0 commit comments