碎片時(shí)間學(xué)編程「47]:理解 JavaScript 中的“this”關(guān)鍵字

是什么this?

在 JavaScript 中,this關(guān)鍵字是指當(dāng)前正在執(zhí)行代碼的對(duì)象。評(píng)估結(jié)果的簡(jiǎn)短版本this如下:

默認(rèn)情況下,this指的是全局對(duì)象。

在函數(shù)中,當(dāng)不處于嚴(yán)格模式時(shí),this指的是全局對(duì)象。

在函數(shù)中,當(dāng)處于嚴(yán)格模式時(shí),this是undefined.

在箭頭函數(shù)中,this保留封閉詞法上下文的值this。

在對(duì)象方法中,this指的是調(diào)用該方法的對(duì)象。

在構(gòu)造函數(shù)調(diào)用中,this綁定到正在構(gòu)造的新對(duì)象。

在事件處理程序中,this綁定到放置偵聽器的元素。

全球背景

在全局執(zhí)行上下文中,this指的是全局對(duì)象。

console.log(this === window); // true

函數(shù)上下文

當(dāng)不在嚴(yán)格模式下時(shí),函數(shù)this引用全局對(duì)象。

function f() {

? return this;

}

console.log(f() === window); // true

在嚴(yán)格模式下,如果在進(jìn)入執(zhí)行上下文時(shí)未設(shè)置函數(shù),則該函數(shù)this將被設(shè)置。undefined

'use strict';

function f() {

? return this;

}

console.log(f()); // undefined

對(duì)象上下文

當(dāng)函數(shù)作為this對(duì)象的方法被調(diào)用時(shí),指的是調(diào)用該方法的對(duì)象。這適用于對(duì)象原型鏈中任何地方定義的方法(即自己的和繼承的方法)。

const obj = {

? f: function() {

? ? return this;

? }

};

const myObj = Object.create(obj);

myObj.foo = 1;

console.log(myObj.f()); // { foo: 1 }

同樣,在構(gòu)造函數(shù)中使用時(shí),this指的是正在構(gòu)造的對(duì)象。

class C {

? constructor() {

? ? this.x = 10;

? }

}

const obj = new C();

console.log(obj.x); // 10

箭頭函數(shù)上下文

在箭頭函數(shù)中,this保留封閉詞法上下文的值this。

const f = () => this;

console.log(f() === window); // true

const obj = {

? foo: function() {

? ? const baz = () => this;

? ? return baz();

? },

? bar: () => this

};

console.log(obj.foo()); // { foo, bar }

console.log(obj.bar() === window); // true

請(qǐng)注意在第二個(gè)示例中,箭頭函數(shù)是如何this引用全局對(duì)象的,除非包含在常規(guī)function調(diào)用中,后者this指的是從中調(diào)用它的對(duì)象,并且其詞法上下文由箭頭函數(shù)保留。

事件處理程序上下文

在事件處理程序中使用時(shí),this指的是放置偵聽器的元素。

const el = document.getElementById('my-el');

el.addEventListener('click', function() {

? console.log(this === el); // true什么是 this?

在 JavaScript 中,this 關(guān)鍵字是指當(dāng)前正在執(zhí)行代碼的對(duì)象。簡(jiǎn)短版本 this 如下:

默認(rèn)情況下,this 指的是全局對(duì)象。

在函數(shù)中,當(dāng)不處于嚴(yán)格模式時(shí),this 指的是全局對(duì)象。

在函數(shù)中,當(dāng)處于嚴(yán)格模式時(shí),this 是 undefined.

在箭頭函數(shù)中,this 保留封閉詞法上下文的值。

在對(duì)象方法中,this 指的是調(diào)用該方法的對(duì)象。

在構(gòu)造函數(shù)調(diào)用中,this 綁定到正在構(gòu)造的新對(duì)象。

在事件處理程序中,this 綁定到放置偵聽器的元素。

全局背景

在全局執(zhí)行上下文中,this 指的是全局對(duì)象。

console.log(this === window); // true

函數(shù)上下文

當(dāng)不在嚴(yán)格模式下時(shí),函數(shù)this引用全局對(duì)象。

function f() {

? return this;

}

console.log(f() === window); // true

在嚴(yán)格模式下,如果在進(jìn)入執(zhí)行上下文時(shí)未設(shè)置函數(shù),則該函數(shù) this 將被設(shè)置為 undefined

'use strict';

function f() {

? return this;

}

console.log(f()); // undefined

對(duì)象上下文

當(dāng)函數(shù)作為this對(duì)象的方法被調(diào)用時(shí),指的是調(diào)用該方法的對(duì)象。這適用于對(duì)象原型鏈中任何地方定義的方法(即自己的和繼承的方法)。

const obj = {

? f: function() {

? ? return this;

? }

};

const myObj = Object.create(obj);

myObj.foo = 1;

console.log(myObj.f()); // { foo: 1 }

同樣,在構(gòu)造函數(shù)中使用時(shí),this 指的是正在構(gòu)造的對(duì)象。

class C {

? constructor() {

? ? this.x = 10;

? }

}

const obj = new C();

console.log(obj.x); // 10

箭頭函數(shù)上下文

在箭頭函數(shù)中,this保留封閉詞法上下文的值。

const f = () => this;

console.log(f() === window); // true

const obj = {

? foo: function() {

? ? const baz = () => this;

? ? return baz();

? },

? bar: () => this

};

console.log(obj.foo()); // { foo, bar }

console.log(obj.bar() === window); // true

請(qǐng)注意在第二個(gè)示例中,箭頭函數(shù)是如何用this引用全局對(duì)象的,除非包含在常規(guī) function 調(diào)用中,后者 this 指的是從中調(diào)用它的對(duì)象,并且其詞法上下文由箭頭函數(shù)保留。

事件處理程序上下文

在事件處理程序中使用時(shí),this 指的是放置偵聽器的元素。

const el = document.getElementById('my-el');

el.addEventListener('click', function() {

? console.log(this === el); // true

});

捆綁this

使用 Function.prototype.bind() 從現(xiàn)有函數(shù)返回一個(gè)新函數(shù),其中 this 永久綁定到 bind() 方法的第一個(gè)參數(shù)。

function f() {

? return this.foo;

}

var x = f.bind({foo: 'hello'});

console.log(x()); // 'hello'

類似地,使用 Function.prototype.call() 或 Function.prototype.apply() 會(huì)將被調(diào)用函數(shù)綁定 this 到這些函數(shù)中的任何一個(gè)的第一個(gè)參數(shù),僅用于此調(diào)用。

function f() {

? return this.foo;

}

console.log(f.call({foo: 'hi'})); // 'hi'

更多內(nèi)容請(qǐng)?jiān)L問(wèn):https://www.icoderoad.com

});

捆綁this

UsingFunction.prototype.bind()從現(xiàn)有函數(shù)返回一個(gè)新函數(shù),其中this永久綁定到 的第一個(gè)參數(shù)bind()。

function f() {

? return this.foo;

}

var x = f.bind({foo: 'hello'});

console.log(x()); // 'hello'

類似地,使用Function.prototype.call()orFunction.prototype.apply()會(huì)將被調(diào)用函數(shù)綁定this到這些函數(shù)中的任何一個(gè)的第一個(gè)參數(shù),僅用于此調(diào)用。

function f() {

? return this.foo;

}

console.log(f.call({foo: 'hi'})); // 'hi'

更多內(nèi)容請(qǐng)?jiān)L問(wèn):https://www.icoderoad.com

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容