JavaScript嚴(yán)格模式下關(guān)于this的幾種指向詳解

前言

“專業(yè)、務(wù)實、高效、創(chuàng)新、把客戶的事當(dāng)成自己的事”是我們每一個人一直以來堅持追求的企業(yè)文化。 創(chuàng)新互聯(lián)是您可以信賴的網(wǎng)站建設(shè)服務(wù)商、專業(yè)的互聯(lián)網(wǎng)服務(wù)提供商! 專注于成都網(wǎng)站設(shè)計、成都做網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、軟件開發(fā)、設(shè)計服務(wù)業(yè)務(wù)。我們始終堅持以客戶需求為導(dǎo)向,結(jié)合用戶體驗與視覺傳達(dá),提供有針對性的項目解決方案,提供專業(yè)性的建議,創(chuàng)新互聯(lián)建站將不斷地超越自我,追逐市場,引領(lǐng)市場!

相信不少人在學(xué)習(xí)或者使用Javascript的時候,都曾經(jīng)被 JavaScript 中的 this 弄暈了,那么本文就來整理總結(jié)一下在嚴(yán)格模式下 this 的幾種指向。

一、全局作用域中的this

在嚴(yán)格模式下,在全局作用域中,this指向window對象

 "use strict";
 
 console.log("嚴(yán)格模式");
 console.log("在全局作用域中的this");
 console.log("this.document === document",this.document === document);
 console.log("this === window",this === window);
 this.a = 9804;
 console.log('this.a === window.a===',window.a);

JavaScript嚴(yán)格模式下關(guān)于this的幾種指向詳解

二、全局作用域中函數(shù)中的this

在嚴(yán)格模式下,這種函數(shù)中的this等于undefined

 "use strict";
 
 console.log("嚴(yán)格模式");
 console.log('在全局作用域中函數(shù)中的this');
 function f1(){
 console.log(this);
 }
 
 function f2(){
 function f3(){
 console.log(this);
 }
 f3();
 }
 f1();
 f2();

JavaScript嚴(yán)格模式下關(guān)于this的幾種指向詳解

三、 對象的函數(shù)(方法)中的this

在嚴(yán)格模式下,對象的函數(shù)中的this指向調(diào)用函數(shù)的對象實例

 "use strict";
 
 console.log("嚴(yán)格模式");
 console.log("在對象的函數(shù)中的this");
 var o = new Object();
 o.a = 'o.a';
 o.f5 = function(){
 return this.a;
 }
 console.log(o.f5());

JavaScript嚴(yán)格模式下關(guān)于this的幾種指向詳解

四、構(gòu)造函數(shù)的this

在嚴(yán)格模式下,構(gòu)造函數(shù)中的this指向構(gòu)造函數(shù)創(chuàng)建的對象實例。

 "use strict";
 
 console.log("嚴(yán)格模式");
 console.log("構(gòu)造函數(shù)中的this");
 function constru(){
 this.a = 'constru.a';
 this.f2 = function(){
 console.log(this.b);
 return this.a;
 }
 }
 var o2 = new constru();
 o2.b = 'o2.b';
 console.log(o2.f2());

JavaScript嚴(yán)格模式下關(guān)于this的幾種指向詳解

五、事件處理函數(shù)中的this

在嚴(yán)格模式下,在事件處理函數(shù)中,this指向觸發(fā)事件的目標(biāo)對象。

 "use strict";
 
 function blue_it(e){
 if(this === e.target){
 this.style.backgroundColor = "#00f";
 }
 }
 var elements = document.getElementsByTagName('*');
 for(var i=0 ; i<elements.length ; i++){
 elements[i].onclick = blue_it;
 }
 
 //這段代碼的作用是使被單擊的元素背景色變?yōu)樗{(lán)色

六、內(nèi)聯(lián)事件處理函數(shù)中的this

在嚴(yán)格模式下,在內(nèi)聯(lián)事件處理函數(shù)中,有以下兩種情況:

 <button onclick="alert((function(){'use strict'; return this})());">
 內(nèi)聯(lián)事件處理1
 </button>
 <!-- 警告窗口中的字符為undefined -->
 
 <button onclick="'use strict'; alert(this.tagName.toLowerCase());">
 內(nèi)聯(lián)事件處理2
 </button>
 <!-- 警告窗口中的字符為button -->

后語

參考資料

MDN https://developer.mozilla.org...

延伸資料

JavaScript 嚴(yán)格模式詳解 https://www.jb51.net/article/74890.htm

菜鳥學(xué)堂 > JavaScript 嚴(yán)格模式 http://edu.jb51.net/js/js-strict.html

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對創(chuàng)新互聯(lián)的支持。

文章標(biāo)題:JavaScript嚴(yán)格模式下關(guān)于this的幾種指向詳解
當(dāng)前地址:http://muchs.cn/article14/iepige.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、小程序開發(fā)、網(wǎng)站導(dǎo)航、網(wǎng)頁設(shè)計公司響應(yīng)式網(wǎng)站、動態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管