本文轉(zhuǎn)載于cnblogs--<<javascript中的this作用域詳解>>
Javascript中this的指向一直是困擾我很久的問題,在使用中出錯(cuò)的機(jī)率也非常大。在面向?qū)ο笳Z言中,它代表了當(dāng)前對(duì)象的一個(gè)引用,而在js中卻經(jīng)常讓我覺得混亂,它不是固定不變的,而是隨著它的執(zhí)行環(huán)境的改變而改變。
在Javascript中this總是指向調(diào)用它所在方法的對(duì)象。因?yàn)閠his是在函數(shù)運(yùn)行時(shí),自動(dòng)生成的一個(gè)內(nèi)部對(duì)象,只能在函數(shù)內(nèi)部使用。
下面我們分幾種情況深入分析this的用法:
1.全局的函數(shù)調(diào)用
function globalTest() {
this.name = "global this";
console.log(this.name);
}
globalTest(); //global this
以上代碼中,globalTest()是全局性的方法,屬于全局性調(diào)用,因此this就代表全局對(duì)象window。為了充分證明this是window,對(duì)代碼做如下更改:
var name = "global this";
function globalTest() {
console.log(this.name);
}
globalTest(); //global this
name作為一個(gè)全局變量,運(yùn)行結(jié)果仍然是“global this”,說明this指向的是window。在方法體中我們嘗試更改全局name,再次調(diào)用方法輸出“rename global this”, 說明全局的name在方法內(nèi)部被更改。代碼如下:
var name = "global this";
function globalTest() {
this.name = "rename global this"
console.log(this.name);
}
globalTest(); //rename global this
根據(jù)以上三段代碼,我們得出結(jié)論:對(duì)于全局的方法調(diào)用,this指向的是全局對(duì)象window,即調(diào)用方法所在的對(duì)象。
2.對(duì)象方法的調(diào)用
如果函數(shù)作為對(duì)象的方法調(diào)用,this指向的是這個(gè)上級(jí)對(duì)象,即調(diào)用方法的對(duì)象。 在以下代碼中,this指向的是obj對(duì)象。
function showName() {
console.log(this.name);
}
var obj = {};
obj.name = "ooo";
obj.show = showName;
obj.show(); //ooo

3.構(gòu)造函數(shù)的調(diào)用
構(gòu)造函數(shù)中的this指向新創(chuàng)建的對(duì)象本身。
function showName() {
this.name = "showName function";
}
var obj = new showName();
console.log(obj.name); //showName function
上述代碼中,我們通過new關(guān)鍵字創(chuàng)建一個(gè)對(duì)象的實(shí)例,new關(guān)鍵字可以改變this的指向,將這個(gè)this指向?qū)ο髈bj。
我們?cè)僭黾右粋€(gè)全局的name,用以證明this指向的不是global:
var name = "global name";
function showName() {
this.name = "showName function";
}
var obj = new showName();
console.log(obj.name); //showName function
console.log(name); //global name
在構(gòu)造函數(shù)的內(nèi)部,我們對(duì)this.name進(jìn)行賦值,但并沒有改變?nèi)肿兞縩ame。
4.apply/call調(diào)用時(shí)的this
apply和call都是為了改變函數(shù)體內(nèi)部的this指向。 其具體的定義如下:
call方法:
語法:call(thisObj,Object)
定義:調(diào)用一個(gè)對(duì)象的一個(gè)方法,以另一個(gè)對(duì)象替換當(dāng)前對(duì)象。
說明:
call 方法可以用來代替另一個(gè)對(duì)象調(diào)用一個(gè)方法。call 方法可將一個(gè)函數(shù)的對(duì)象上下文從初始的上下文改變?yōu)橛?thisObj 指定的新對(duì)象。
如果沒有提供 thisObj 參數(shù),那么 Global 對(duì)象被用作 thisObj。
apply方法:
語法:apply(thisObj,[argArray])
定義:應(yīng)用某一對(duì)象的一個(gè)方法,用另一個(gè)對(duì)象替換當(dāng)前對(duì)象。
說明:
如果 argArray 不是一個(gè)有效的數(shù)組或者不是 arguments 對(duì)象,那么將導(dǎo)致一個(gè) TypeError。
如果沒有提供 argArray 和 thisObj 任何一個(gè)參數(shù),那么 Global 對(duì)象將被用作 thisObj, 并且無法被傳遞任何參數(shù)。
var value = "Global value";
function FunA() {
this.value = "AAA";
}
function FunB() {
console.log(this.value);
}
FunB(); //Global value 因?yàn)槭窃谌种姓{(diào)用的FunB(),this.value指向全局的value
FunB.call(window); //Global value,this指向window對(duì)象,因此this.value指向全局的value
FunB.call(new FunA()); //AAA, this指向參數(shù)new FunA(),即FunA對(duì)象
FunB.apply(window); //Global value
FunB.apply(new FunA()); //AAA
在上述代碼中,this的指向在call和apply中是一致的,只不過是調(diào)用參數(shù)的形式不一樣。call是一個(gè)一個(gè)調(diào)用參數(shù),而apply是調(diào)用一個(gè)數(shù)組。