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

前端百題斬——快速手撕Call、Apply、Bind

開(kāi)發(fā) 前端
call() 方法使用一個(gè)指定的 this 值和單獨(dú)給出的一個(gè)或多個(gè)參數(shù)來(lái)調(diào)用一個(gè)函數(shù)。其返回值是使用調(diào)用者提供的this值和參數(shù)調(diào)用該函數(shù)的返回值,若該方法沒(méi)有返回值,則返回undefined。

[[404584]]

在百題斬js中的這些“this”指向都值得了解中已經(jīng)簡(jiǎn)要概述了call、apply、bind三個(gè)方法,這三者作用是相同的,均可以改變this指向,從而讓某對(duì)象可以調(diào)用自身不具備的方法,本節(jié)將深入理解這三者的實(shí)現(xiàn)原理。

15.1 call()

15.1.1 基礎(chǔ)

call() 方法使用一個(gè)指定的 this 值和單獨(dú)給出的一個(gè)或多個(gè)參數(shù)來(lái)調(diào)用一個(gè)函數(shù)。其返回值是使用調(diào)用者提供的this值和參數(shù)調(diào)用該函數(shù)的返回值,若該方法沒(méi)有返回值,則返回undefined。

基本用法:

  1. function.call(thisArg, arg1, arg2, ...) 

小試牛刀

  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 

15.1.2 實(shí)現(xiàn)

實(shí)現(xiàn)一個(gè)call函數(shù),將通過(guò)以下幾個(gè)步驟:

  1. 獲取第一個(gè)參數(shù)(注意第一個(gè)參數(shù)為null或undefined時(shí),this指向window),構(gòu)建對(duì)象
  2. 將對(duì)應(yīng)函數(shù)傳入該對(duì)象中
  3. 獲取參數(shù)并執(zhí)行相應(yīng)函數(shù)
  4. 刪除該對(duì)象中函數(shù),消除副作用
  5. 返回結(jié)果
  1. Function.prototype.myCall = function (context, ...args) { 
  2.     // 獲取第一個(gè)參數(shù)(注意第一個(gè)參數(shù)為null或undefined時(shí),this指向window),構(gòu)建對(duì)象 
  3.     context = context ? Object(context) : window; 
  4.     // 將對(duì)應(yīng)函數(shù)傳入該對(duì)象中 
  5.     context.fn = this; 
  6.     // 獲取參數(shù)并執(zhí)行相應(yīng)函數(shù) 
  7.     let result = context.fn(...args); 
  8.     // 消除副作用 
  9.     delete context.fn; 
  10.     // 返回結(jié)果 
  11.     return result; 
  12. // …… 
  13. console.log(method.myCall(obj, 3, 4)); // 10 

15.2 apply()

15.2.1 基礎(chǔ)

apply() 方法調(diào)用一個(gè)具有給定this值的函數(shù),以及以一個(gè)數(shù)組(或類(lèi)數(shù)組對(duì)象)的形式提供的參數(shù)。其返回值是指定this值和參數(shù)的函數(shù)的結(jié)果。call() 和 apply()的區(qū)別是call()方法接受的是參數(shù)列表,而apply()方法接受的是一個(gè)參數(shù)數(shù)組;

基本用法

  1. func.apply(thisArg, [argsArray]) 

小試牛刀

  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 

15.2.2 實(shí)現(xiàn)

apply和call的區(qū)別主要是參數(shù)的不同,所以其實(shí)現(xiàn)步驟的call大體類(lèi)似,如下所示:

  1. Function.prototype.myApply = function (context, arr) { 
  2.     context = context ? Object(context) : window; 
  3.     context.fn = this; 
  4.  
  5.     let result = arr ? context.fn(...arr) : context.fun(); 
  6.  
  7.     delete context.fn; 
  8.  
  9.     return result; 
  10. // …… 
  11. console.log(method.myApply(obj, [3, 4])); // 10 

15.3 bind()

15.3.1 基礎(chǔ)

bind() 方法創(chuàng)建一個(gè)新的函數(shù),在 bind() 被調(diào)用時(shí),這個(gè)新函數(shù)的 this 被指定為 bind() 的第一個(gè)參數(shù),而其余參數(shù)將作為新函數(shù)的參數(shù),供調(diào)用時(shí)使用。該函數(shù)的返回值是一個(gè)原函數(shù)的拷貝,并擁有指定的this值和初始參數(shù)。

基本用法

  1. function.bind(thisArg[, arg1[, arg2[, ...]]]) 

小試牛刀

  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()); // 10 

15.3.2 實(shí)現(xiàn)

實(shí)現(xiàn)一個(gè)bind函數(shù)相對(duì)較復(fù)雜一些,應(yīng)該注意以下幾點(diǎn):

  1. 能夠改變this指向;
  2. 返回的是一個(gè)函數(shù);
  3. 能夠接受多個(gè)參數(shù);
  4. 支持柯里化形式傳參 fun(arg1)(arg2);
  5. 獲取到調(diào)用bind()返回值后,若使用new調(diào)用(當(dāng)做構(gòu)造函數(shù)),bind()傳入的上下文context失效。
  1. Function.prototype.myBind = function (context, ...args) { 
  2.     if (typeof(this) !== 'function') { 
  3.         throw new TypeError('The bound object needs to be a function'); 
  4.     } 
  5.  
  6.     const self = this; 
  7.     // 定義一個(gè)中裝函數(shù) 
  8.     const fNOP = function() {}; 
  9.     const fBound = function(...fBoundArgs) { 
  10.         // 利用apply改變this指向 
  11.         // 接受多個(gè)參數(shù)+支持柯里化形式傳參 
  12.         // 當(dāng)返回值通過(guò)new調(diào)用時(shí),this指向當(dāng)前實(shí)例 (因?yàn)閠his是當(dāng)前實(shí)例,實(shí)例的隱士原型上有fNOP的實(shí)例(fnop);fnop instanceof fNOP為true) 
  13.         return self.apply(this instanceof fNOP ? this : context, [...args, ...fBoundArgs]); 
  14.     } 
  15.  
  16.     // 將調(diào)用函數(shù)的原型賦值到中轉(zhuǎn)函數(shù)的原型上 
  17.     if (this.prototype) { 
  18.         fNOP.prototype = this.prototype; 
  19.     } 
  20.     // 通過(guò)原型的方式繼承調(diào)用函數(shù)的原型 
  21.     fBound.prototype = new fNOP(); 
  22.  
  23.     return fBound; 

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

 

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

2021-06-16 07:03:37

New操作符函數(shù)

2024-03-15 08:21:17

bindJavaScrip函數(shù)

2021-12-03 06:59:23

操作符驗(yàn)證點(diǎn)屬性

2021-10-19 22:23:05

typeof方式Instanceof

2021-05-09 22:00:59

TypeofInstanceof運(yùn)算符

2021-12-05 08:27:56

Javascript 高階函數(shù)前端

2021-06-18 07:16:17

JavaScript apply()方法call()方法

2021-05-30 19:02:59

變量對(duì)象上下文

2017-10-10 14:36:07

前端Javascriptapply、call、

2024-08-26 14:35:19

JavaScript關(guān)鍵字對(duì)象

2015-03-02 09:22:09

Javascript函數(shù)用法apply

2021-08-04 06:56:49

HTTP緩存前端

2021-10-18 09:01:01

前端賦值淺拷貝

2021-07-14 07:00:53

瀏覽器技巧前端

2021-11-30 06:56:58

CallApply函數(shù)

2021-07-26 05:01:55

瀏覽器渲染流程

2021-06-28 07:12:28

賦值淺拷貝深拷貝

2021-11-19 09:01:09

防抖節(jié)流前端

2021-05-12 07:04:55

Js變量方式

2021-06-04 07:04:29

閉包JavaScript函數(shù)
點(diǎn)贊
收藏

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