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

解析 Call/Apply 原理,并手寫(xiě) Call/Apply 實(shí)現(xiàn)

開(kāi)發(fā) 前端
apply() 方法調(diào)用一個(gè)具有給定 this 值的函數(shù),以及作為一個(gè)數(shù)組(或[類(lèi)似數(shù)組對(duì)象)提供的參數(shù)。

[[437510]]

本文轉(zhuǎn)載自微信公眾號(hào)「三分鐘學(xué)前端」,作者sisterAn  。轉(zhuǎn)載本文請(qǐng)聯(lián)系三分鐘學(xué)前端公眾號(hào)。

Function.prototype.call()

call() 方法調(diào)用一個(gè)函數(shù), 其具有一個(gè)指定的 this 值和多個(gè)參數(shù)(參數(shù)的列表)。

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

它運(yùn)行 func,提供的第一個(gè)參數(shù) thisArg 作為 this,后面的作為參數(shù)。

看一個(gè)簡(jiǎn)單的例子:

  1. function sayWord() { 
  2.   var talk = [this.name'say', this.word].join(' '); 
  3.   console.log(talk); 
  4.  
  5. var bottle = { 
  6.   name'bottle',  
  7.   word: 'hello' 
  8. }; 
  9.  
  10. // 使用 call 將 bottle 傳遞為 sayWord 的 this 
  11. sayWord.call(bottle);  
  12. // bottle say hello 

所以,call 主要實(shí)現(xiàn)了以下兩個(gè)功能:

  • call 改變了 this 的指向
  • bottle 執(zhí)行了 sayWord 函數(shù)

模擬實(shí)現(xiàn) call

模擬實(shí)現(xiàn) call 有三步:

  • 將函數(shù)設(shè)置為對(duì)象的屬性
  • 執(zhí)行函數(shù)
  • 刪除對(duì)象的這個(gè)屬性
  1. Function.prototype.call = function (context) { 
  2.   // 將函數(shù)設(shè)為對(duì)象的屬性 
  3.   // 注意:非嚴(yán)格模式下,  
  4.   //   指定為 null 和 undefined 的 this 值會(huì)自動(dòng)指向全局對(duì)象(瀏覽器中就是 window 對(duì)象) 
  5.   //   值為原始值(數(shù)字,字符串,布爾值)的 this 會(huì)指向該原始值的自動(dòng)包裝對(duì)象(用 Object() 轉(zhuǎn)換) 
  6.   context = context ? Object(context) : window;  
  7.   context.fn = this; 
  8.      
  9.   // 執(zhí)行該函數(shù) 
  10.   let args = [...arguments].slice(1); 
  11.   let result = context.fn(...args); 
  12.      
  13.   // 刪除該函數(shù) 
  14.   delete context.fn 
  15.   // 注意:函數(shù)是可以有返回值的 
  16.   return result; 

Function.prototype.apply()

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

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

它運(yùn)行 func 設(shè)置 this = context 并使用類(lèi)數(shù)組對(duì)象 args 作為參數(shù)列表。

例如,這兩個(gè)調(diào)用幾乎相同:

  1. func(1, 2, 3); 
  2. func.apply(context, [1, 2, 3]) 

兩個(gè)都運(yùn)行 func 給定的參數(shù)是 1,2,3。但是 apply 也設(shè)置了 this = context。

call 和 apply 之間唯一的語(yǔ)法區(qū)別是 call 接受一個(gè)參數(shù)列表,而 apply 則接受帶有一個(gè)類(lèi)數(shù)組對(duì)象。

需要注意:Chrome 14 以及 Internet Explorer 9 仍然不接受類(lèi)數(shù)組對(duì)象。如果傳入類(lèi)數(shù)組對(duì)象,它們會(huì)拋出異常。

模擬實(shí)現(xiàn) apply

  1. Function.prototype.apply = function (context, arr) { 
  2.     context = context ? Object(context) : window;  
  3.     context.fn = this; 
  4.    
  5.     let result; 
  6.     if (!arr) { 
  7.         result = context.fn(); 
  8.     } else { 
  9.         result = context.fn(...arr); 
  10.     } 
  11.        
  12.     delete context.fn 
  13.     return result; 

 

責(zé)任編輯:武曉燕 來(lái)源: 三分鐘學(xué)前端
相關(guān)推薦

2024-03-15 08:21:17

bindJavaScrip函數(shù)

2024-08-26 14:35:19

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

2021-12-05 08:27:56

Javascript 高階函數(shù)前端

2011-03-22 09:49:15

JavaScript

2021-06-18 07:16:17

JavaScript apply()方法call()方法

2017-10-10 14:36:07

前端Javascriptapply、call、

2021-06-09 07:01:30

前端CallApply

2015-03-02 09:22:09

Javascript函數(shù)用法apply

2024-08-20 16:04:27

JavaScript開(kāi)發(fā)

2024-08-26 08:36:26

2021-12-01 06:40:32

Bind原理實(shí)現(xiàn)

2011-08-15 12:55:54

SQL ServerOUTER APPLYCROSS APPLY

2022-02-17 20:57:07

OpenHarmon操作系統(tǒng)鴻蒙

2009-06-24 11:12:17

callerJavascript

2020-12-18 05:42:46

reduxactions

2015-12-24 09:48:40

JavaScriptthis指針深

2022-07-27 08:27:34

Call前端

2021-04-18 07:58:22

SQL Server數(shù)據(jù)庫(kù)Apply

2022-06-16 11:06:07

開(kāi)源Grafanaon-call

2022-05-26 08:12:39

PandasApply技巧
點(diǎn)贊
收藏

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