// Situation 1
var a = function A() {
this.x = 1;
var b = function B() {
this.x = 2;
console.log('Method B : x = ' + this.x);
};
console.log('Method A : x = ' + this.x);
b();
}
當我叫(),我的結果是「這個」,在函數調用的javascript
Method A : x = 1
Method B : x = 2
但如果我刪除 「this.x = 2」:
// Situation 2
var a = function A() {
this.x = 1;
var b = function B() {
console.log('Method B : x = ' + this.x);
};
console.log('Method A : x = ' + this.x);
b();
}
我的結果將是
Method A : x = 1
Method B : x = 1
我不明白爲什麼
- 在情況2: 「這」 函數B的被引用到的 「本」 功能的
但是
- 在情況1:功能A的 「this.x」不改變時分配 「this.x = 2」,在函數B
我的代碼在Chrome V23運行
我想當分配「var a = function A()」時,創建函數對象A()。然後,在函數A()中的「this」引用對象A,但我不明白爲什麼「this」引用__global對象_? –