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

Bind、Call、Apply的區(qū)別?如何實現(xiàn)bind

開發(fā) 前端
bind() 方法創(chuàng)建一個新的函數(shù),該函數(shù)的 this 關(guān)鍵字被綁定到指定的對象,并且提供了一系列參數(shù)。不會立即執(zhí)行函數(shù),而是返回一個新的函數(shù),可以稍后調(diào)用。

bind、call、apply的作用?

bind, call, 和 apply 是 JavaScript 中非常有用的方法,它們主要用于改變函數(shù)的執(zhí)行上下文以及傳遞參數(shù)。

  • bind:bind()方法創(chuàng)建一個新的函數(shù),該函數(shù)的this關(guān)鍵字被綁定到指定的對象,同時還可以提供一系列參數(shù)。這對于在事件處理函數(shù)、定時器或回調(diào)函數(shù)中綁定上下文非常有用。
const obj = {
  x: 42
};


function getX(y) {
  return this.x + y;
}


const boundGetX = getX.bind(obj);
console.log(boundGetX(2)); // 輸出 44
  • call:call()方法調(diào)用一個函數(shù),允許你指定函數(shù)執(zhí)行時的上下文(this),并傳遞一系列參數(shù)作為函數(shù)的參數(shù)。這在需要立即調(diào)用函數(shù)并指定上下文的情況下非常有用。
const obj = {
  x: 42
};


function getX(y) {
  return this.x + y;
}


console.log(getX.call(obj, 2)); // 輸出 44
  • apply:apply()方法調(diào)用一個函數(shù),允許你指定函數(shù)執(zhí)行時的上下文(this),同時傳遞一個數(shù)組或類數(shù)組對象作為函數(shù)的參數(shù)。這在需要傳遞參數(shù)數(shù)組的情況下非常有用。
const obj = {
  x: 42
};


function getX(y) {
  return this.x + y;
}


console.log(getX.apply(obj, [2])); // 輸出 44

bind、call、apply的區(qū)別?

bind, call, 和 apply 是 JavaScript 中用于處理函數(shù)執(zhí)行上下文和參數(shù)傳遞的方法,它們有著不同的特點和用途。

  • bind()

bind() 方法創(chuàng)建一個新的函數(shù),該函數(shù)的 this 關(guān)鍵字被綁定到指定的對象,并且提供了一系列參數(shù)。不會立即執(zhí)行函數(shù),而是返回一個新的函數(shù),可以稍后調(diào)用。

const obj = {
  x: 42
};


function getX(y) {
  return this.x + y;
}


const boundGetX = getX.bind(obj);
console.log(boundGetX(2)); // 輸出 44
  • call()

call() 方法調(diào)用一個函數(shù),允許你顯式指定函數(shù)執(zhí)行時的上下文(this),并且可以傳遞一系列參數(shù)作為函數(shù)的參數(shù)。立即執(zhí)行函數(shù)。

const obj = {
  x: 42
};


function getX(y) {
  return this.x + y;
}


console.log(getX.call(obj, 2)); // 輸出 44
  • apply()

apply() 方法調(diào)用一個函數(shù),允許你顯式指定函數(shù)執(zhí)行時的上下文(this),同時傳遞一個數(shù)組或類數(shù)組對象作為函數(shù)的參數(shù)。立即執(zhí)行函數(shù)。

const obj = {
  x: 42
};


function getX(y) {
  return this.x + y;
}


console.log(getX.apply(obj, [2])); // 輸出 44

區(qū)別總結(jié):

  • 參數(shù)傳遞方式:

bind() 接受一系列參數(shù),返回一個新函數(shù)。

call() 和 apply() 接受一個參數(shù)列表或數(shù)組作為參數(shù)。

  • 執(zhí)行時機:
  • bind() 不會立即執(zhí)行函數(shù),而是返回一個新的綁定函數(shù)。
  • call() 和 apply() 立即執(zhí)行函數(shù)。
  • 返回值:
  • bind() 返回一個新的函數(shù)。
  • call() 和 apply() 直接執(zhí)行函數(shù),并返回執(zhí)行結(jié)果。

實現(xiàn)

下面是一個簡單的 bind 函數(shù)的實現(xiàn),該實現(xiàn)基于了對 JavaScript 的原型鏈和閉包的理解:

Function.prototype.myBind = function (context) {
  const fn = this; // 保存原函數(shù)
  const args = Array.prototype.slice.call(arguments, 1); // 獲取除第一個參數(shù)(context)以外的所有參數(shù)
  return function () { // 返回一個函數(shù),這個函數(shù)會被當(dāng)做綁定后的函數(shù)調(diào)用
    const bindArgs = Array.prototype.slice.call(arguments); // 獲取 bind 方法的參數(shù)
    return fn.apply(context, args.concat(bindArgs)); // 在 context 上執(zhí)行原函數(shù),并傳入所有參數(shù)
  };
};


// 示例
const obj = {
  x: 42
};


function getX(y) {
  return this.x + y;
}


const boundGetX = getX.myBind(obj);
console.log(boundGetX(2)); // 輸出 44

在這個實現(xiàn)中,通過 Function.prototype 對象擴展了一個 myBind 方法。在 myBind 方法內(nèi)部,首先保存了原函數(shù) fn,然后提取除第一個參數(shù)(要綁定的上下文)之外的所有參數(shù)到 args 數(shù)組中。然后,我們返回了一個新的函數(shù),這個函數(shù)會在指定的上下文 context 上執(zhí)行原函數(shù),并將原始的參數(shù)與綁定的參數(shù)合并起來傳遞給原函數(shù)。

責(zé)任編輯:武曉燕 來源: 海燕技術(shù)棧
相關(guān)推薦

2024-08-26 14:35:19

JavaScript關(guān)鍵字對象

2021-12-05 08:27:56

Javascript 高階函數(shù)前端

2024-08-20 16:04:27

JavaScript開發(fā)

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-26 08:36:26

2021-12-01 06:40:32

Bind原理實現(xiàn)

2021-11-30 06:56:58

CallApply函數(shù)

2011-03-17 15:48:32

jQuery

2011-08-25 13:58:08

bind中文man

2022-07-13 09:00:06

BindNew 操作

2020-03-16 09:10:41

bindDNS服務(wù)器系統(tǒng)運維

2011-03-22 09:49:15

JavaScript

2009-01-14 17:46:01

RHELBindDNS

2013-03-01 11:17:38

BIND10DNS

2021-05-11 09:37:00

JsBind代碼

2009-07-30 15:09:44

asp.net中Bin

2021-05-12 10:46:23

漏洞BINDDNS服務(wù)器
點贊
收藏

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