自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

前端百題斬——js中的這些“this”指向都值得了解

開發(fā) 前端
this是javascript中的一個(gè)關(guān)鍵字,其使用方法類似于一個(gè)變量,是執(zhí)行上下文中一個(gè)重要組成部分。其作用是可以在函數(shù)體內(nèi)部獲取當(dāng)前的運(yùn)行環(huán)境。

[[403989]]

14.1 簡介

this是javascript中的一個(gè)關(guān)鍵字,其使用方法類似于一個(gè)變量,是執(zhí)行上下文中一個(gè)重要組成部分。其作用是可以在函數(shù)體內(nèi)部獲取當(dāng)前的運(yùn)行環(huán)境。

14.2 指向

每個(gè)函數(shù)的this是在調(diào)用的時(shí)候基于函數(shù)的執(zhí)行環(huán)境綁定的,this的指向完全取決于函數(shù)的調(diào)用位置。(下面均是在瀏覽器環(huán)境下進(jìn)行測試的結(jié)果)

在全局環(huán)境下,this 始終指向全局對(duì)象(window), 無論是否嚴(yán)格模式;

  1. console.log(this); // window 

普通函數(shù)內(nèi)部的this分兩種情況,嚴(yán)格模式和非嚴(yán)格模式。

(1)非嚴(yán)格模式下,this 默認(rèn)指向全局對(duì)象window

(2)嚴(yán)格模式下, this為undefined

  1. function fun() { 
  2.     console.log(this); // window 

對(duì)象內(nèi)部方法的this指向調(diào)用這些方法的對(duì)象

(1)函數(shù)的定義位置不影響其this指向,this指向只和調(diào)用函數(shù)的對(duì)象有關(guān);

(2)多層嵌套的對(duì)象,內(nèi)部方法的this指向離被調(diào)用函數(shù)最近的對(duì)象(window也是對(duì)象,其內(nèi)部對(duì)象調(diào)用方法的this指向內(nèi)部對(duì)象, 而非window)。

  1. const obj = { 
  2.     a: 10, 
  3.     b: 20, 
  4.     addfunction () { 
  5.         return this.a + this.b; 
  6.     } 
  7. }; 
  8.  
  9. console.log(obj.add()); // 30 
  10. const add = obj.add
  11. console.log(add()); // NaN 

原型鏈中的方法的this仍然指向調(diào)用它的對(duì)象

  1. const obj = { 
  2.     a: 10, 
  3.     b: 20 
  4. }; 
  5.  
  6. const prototypeObj = { 
  7.     addfunction () { 
  8.         return this.a + this.b; 
  9.     } 
  10. }; 
  11.  
  12. Object.setPrototypeOf(obj, prototypeObj); 
  13.  
  14. console.log(obj.add()); // 30 

當(dāng)函數(shù)通過Function對(duì)象的原型中繼承的方法 call() 和 apply() 方法調(diào)用時(shí), 其函數(shù)內(nèi)部的this值可綁定到 call() & apply() 方法指定的第一個(gè)對(duì)象上, 如果第一個(gè)參數(shù)不是對(duì)象,JavaScript內(nèi)部會(huì)嘗試將其轉(zhuǎn)換成對(duì)象然后指向它。(見后續(xù)代碼)

通過bind方法綁定后, 函數(shù)將被永遠(yuǎn)綁定在其第一個(gè)參數(shù)對(duì)象上, 而無論其在什么情況下被調(diào)用。(見后續(xù)代碼)

當(dāng)函數(shù)被當(dāng)做監(jiān)聽事件處理函數(shù)時(shí), 其 this 指向觸發(fā)該事件的元素(針對(duì)于addEventListener事件)

  1. <button id="testId">按鈕</button> 
  2.  
  3. const btn = document.getElementById('testId'); 
  4. btn.addEventListener('click'function() { 
  5.  console.log(this); // <button id="testId">按鈕</button> 
  6. }); 

內(nèi)聯(lián)事件中的this指向分兩種情況:

(1)當(dāng)代碼被內(nèi)聯(lián)處理函數(shù)調(diào)用時(shí),它的this指向監(jiān)聽器所在的DOM元素

  1. <button onclick="console.log(this)">按鈕</button> // 輸出該DOM節(jié)點(diǎn) 

(2)當(dāng)代碼被包括在函數(shù)內(nèi)部執(zhí)行時(shí),其this指向等同于 函數(shù)直接調(diào)用的情況,即在非嚴(yán)格模式指向全局對(duì)象window, 在嚴(yán)格模式指向undefined

  1. <button onclick="clickFun()">按鈕</button> 
  2.  
  3. function clickFun() { 
  4.  console.log(this); // window 

對(duì)于延時(shí)函數(shù)內(nèi)部的回調(diào)函數(shù)的this指向全局對(duì)象window(當(dāng)然可以通過bind方法改變其內(nèi)部函數(shù)的this指向)

  1. function Fun() { 
  2.     this.a = 10; 
  3.     this.method = function() { 
  4.         setTimeout(function() { 
  5.             console.log(this); // window 
  6.         }, 1000); 
  7.     } 
  8.  
  9. const fun = new Fun(); 
  10. fun.method(); 

由于箭頭函數(shù)不綁定this, 它會(huì)捕獲其所在(即定義的位置)上下文的this值, 作為自己的this值,所以 call() / apply() / bind() 方法對(duì)于箭頭函數(shù)來說只是傳入?yún)?shù),對(duì)它的 this 毫無影響。

  1. function Fun() { 
  2.     this.a = 10; 
  3.     this.method = function() { 
  4.         setTimeout(() => { 
  5.             console.log(this); // Fun {a: 10, method: ƒ} 
  6.         }, 1000); 
  7.     } 
  8.  
  9. const fun = new Fun(); 
  10. fun.method(); 

14.3 改變this指向

除了隱式綁定this的方式,還能夠通過顯示綁定的方式,通過call、apply、bind方式改變this指向,對(duì)于這三者的區(qū)別后續(xù)將有專門的百題斬去闡述,本節(jié)主要進(jìn)行一波簡單使用。

call()

call() 方法使用一個(gè)指定的 this 值和單獨(dú)給出的一個(gè)或多個(gè)參數(shù)來調(diào)用一個(gè)函數(shù)。

  1. function method(val1, val2) { 
  2.     return this.a + this.b + val1 + val2; 
  3.  
  4. const obj = { 
  5.     a: 1, 
  6.     b: 2 
  7. }; 
  8.  
  9. console.log(method.call(obj, 3, 4)); // 10 

apply()

apply() 方法調(diào)用一個(gè)具有給定this值的函數(shù),以及以一個(gè)數(shù)組(或類數(shù)組對(duì)象)的形式提供的參數(shù)。

  1. function method(val1, val2) { 
  2.     return this.a + this.b + val1 + val2; 
  3.  
  4. const obj = { 
  5.     a: 1, 
  6.     b: 2 
  7. }; 
  8.  
  9. console.log(method.apply(obj, [3, 4])); // 10 

bind()

bind() 方法創(chuàng)建一個(gè)新的函數(shù),在 bind() 被調(diào)用時(shí),這個(gè)新函數(shù)的 this 被指定為 bind() 的第一個(gè)參數(shù),而其余參數(shù)將作為新函數(shù)的參數(shù),供調(diào)用時(shí)使用。

  1. function method(val1, val2) { 
  2.     return this.a + this.b + val1 + val2; 
  3.  
  4. const obj = { 
  5.     a: 1, 
  6.     b: 2 
  7. }; 
  8.  
  9. const bindMethod = method.bind(obj, 3, 4); 
  10. console.log(bindMethod); // [Function: bound method] 
  11. console.log(bindMethod()); // 10 

擴(kuò)展

 

  • call() 和 apply()的區(qū)別是call()方法接受的是參數(shù)列表,而apply()方法接受的是一個(gè)參數(shù)數(shù)組;
  • bind返回的是一個(gè)綁定函數(shù),而call和apply返回的是運(yùn)行結(jié)果;
  • 多次 bind() 是無效的,只會(huì)綁定到第一次調(diào)用的對(duì)象上;
  • call() / apply() / bind() 方法對(duì)于箭頭函數(shù)來說只是傳入?yún)?shù),對(duì)它的 this 毫無影響。

本文轉(zhuǎn)載自微信公眾號(hào)「執(zhí)鳶者」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系執(zhí)鳶者公眾號(hào)。

 

責(zé)任編輯:武曉燕 來源: 執(zhí)鳶者
相關(guān)推薦

2021-05-19 07:02:42

JS對(duì)象方法

2021-05-12 07:04:55

Js變量方式

2021-08-04 06:56:49

HTTP緩存前端

2021-07-14 07:00:53

瀏覽器技巧前端

2021-06-02 07:02:42

js作用域函數(shù)

2021-10-19 22:23:05

typeof方式Instanceof

2021-05-09 22:00:59

TypeofInstanceof運(yùn)算符

2021-05-30 19:02:59

變量對(duì)象上下文

2021-06-09 07:01:30

前端CallApply

2021-07-19 07:02:10

瀏覽器進(jìn)程單進(jìn)程瀏覽器

2021-11-19 09:01:09

防抖節(jié)流前端

2014-08-19 14:12:47

Windows

2021-07-26 05:01:55

瀏覽器渲染流程

2021-10-18 09:01:01

前端賦值淺拷貝

2018-10-09 14:34:58

開源KubernetesGit

2021-05-27 07:02:05

JavaScript代碼設(shè)施

2021-07-05 07:02:33

前端跨域策略

2021-06-11 06:54:34

原型構(gòu)造函數(shù)

2021-06-04 07:04:29

閉包JavaScript函數(shù)

2021-01-07 05:40:13

BLE模塊Android
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)