亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

如何使用50行javaScript代碼實(shí)現(xiàn)簡(jiǎn)單版的call,apply,bind

 更新時(shí)間:2019年08月14日 11:50:54   作者:Peter譚金杰  
這篇文章主要介紹了50行javaScript代碼實(shí)現(xiàn)簡(jiǎn)單版的call,apply,bind過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

在實(shí)現(xiàn)自己的call,apply,bind前,需要復(fù)習(xí)一下this.

所謂的this其實(shí)可以理解成一根指針:

其實(shí) this 的指向,始終堅(jiān)持一個(gè)原理:this 永遠(yuǎn)指向最后調(diào)用它的那個(gè)對(duì)象,這就是精髓。最關(guān)鍵所在

this的四種指向:

當(dāng)this所在的函數(shù)被普通調(diào)用時(shí),指向window,如果當(dāng)前是嚴(yán)格模式,則指向undefined

function test() {
 console.log(this);
};

test();
指向window 輸出下面的代碼:
// Window {speechSynthesis: SpeechSynthesis, caches: CacheStorage, localStorage: Storage, sessionStorage: Storage, webkitStorageInfo: DeprecatedStorageInfo…}
嚴(yán)格模式
'use strict';
function test() {
 console.log(this);
};
test();
// undefined

當(dāng)this所在當(dāng)函數(shù)被以obj.fn()形式調(diào)用時(shí),指向obj

var obj = {
 name: 'segmentFault',
 foo: function() {
  console.log(this.name);
 }
}
obj.foo();
// 'segmentFault'

還可以這么做

function test() {
 console.log(this.name);
}
var obj = {
 name: 'qiutc',
 foo: test
}
obj.foo();
// 'qiutc'

當(dāng)call,apply加入后,this的指向被改變了

 function a(a,b,c) {
    console.log(this.name);
    console.log(a,b,c)
  }
  const b = {
    name: "segmentFault"
  }
  a.call(b,1,2,3)    
  //輸出 segmentFault和 1,2,3
  function a(a,b,c) {
    console.log(this.name);
    console.log(a,b,c)
  }
  a.apply(b,[1,2,3])
  //輸出segmentFault和1,2,3

遇到bind后 :

  function a() {
    console.log(this.name);
  }
  const b = {
    name: "segmentFault"
  }
  a.bind(b, 1, 2, 3)

此時(shí)控制臺(tái)并沒有代碼輸出,因?yàn)閎ind會(huì)重新生成并且返回一個(gè)函數(shù),這個(gè)函數(shù)的this指向第一個(gè)參數(shù)

  function a() {
    console.log(this.name);
  }
  const b = {
    name: "segmentFault"
  }
  const c = a.bind(b, 1, 2, 3)
  c()
  //此時(shí)輸出segmentFault

正式開始自己實(shí)現(xiàn)call :

在函數(shù)原型上定義自己的myCall方法:

 Function.prototype.myCall = function (context, ...arg) {
    const fn = Symbol('臨時(shí)屬性')
    context[fn] = this
    context[fn](...arg)
    delete context[fn]
  }

四行代碼實(shí)現(xiàn)了簡(jiǎn)單的call,思路如下:

  • 通過對(duì)象屬性的方式調(diào)用函數(shù),這個(gè)函數(shù)里面的this指向這個(gè)對(duì)象
  • 每次調(diào)用新增一個(gè)symbol屬性,調(diào)用完畢刪除
  • 這個(gè)symbol屬性就是調(diào)用mycall方法的函數(shù)
  • 函數(shù)形參中使用...arg是將多個(gè)形參都塞到一個(gè)數(shù)組里,在函數(shù)內(nèi)部使用arg這個(gè)變量時(shí),就是包含所有形參的數(shù)組
  • 在調(diào)用 context[fn](...arg)時(shí)候,...arg是為了展開數(shù)組,依次傳入?yún)?shù)調(diào)用函數(shù)

為了簡(jiǎn)化,今天都不做類型判斷和錯(cuò)誤邊際處理,只把原理講清楚。

自己實(shí)現(xiàn)apply

在函數(shù)原型上定義自己的myApply方法:

//實(shí)現(xiàn)自己的myApply
  Function.prototype.myApply = function (context, arg) {
    const fn = Symbol('臨時(shí)屬性')
    context[fn] = this
    context[fn](...arg)
    delete context[fn]
  }
  const obj2 = {
    a: 1
  }
  test.myApply(obj2, [2, 3, 4])

同理,只是apply傳遞的第二個(gè)參數(shù)是數(shù)組,這里我們只需要在調(diào)用時(shí),將參數(shù)用...把數(shù)組展開即可

自己實(shí)現(xiàn)bind:

bind跟apply,call的本質(zhì)區(qū)別,bind不會(huì)改變?cè)瘮?shù)的this指向,只會(huì)返回一個(gè)新的函數(shù)(我們想要的那個(gè)this指向),并且不會(huì)調(diào)用。但是apply和bind會(huì)改變?cè)瘮?shù)的this指向并且直接調(diào)用

bind在編寫框架源碼,例如koa等中用得特別多:

 //實(shí)現(xiàn)自己的myBind
  Function.prototype.myBind = function (context, ...firstarg) {
    const that = this
    const bindFn = function (...secoundarg) {
      return that.myCall(context, ...firstarg, ...secoundarg)
    }
    bindFn.prototype = Object.create(that.prototype)
    return bindFn
  }

  var fnbind = test.myBind(obj, 2)
  fnbind(3)

同理 自己定義好原型上的myBind方法

this劫持 保留最初的調(diào)用mybind方法的那個(gè)對(duì)象

返回一個(gè)新的函數(shù) 這個(gè)新的函數(shù)內(nèi)部this指向已經(jīng)確定,使用的是我們的mycall方法

學(xué)習(xí)需要循序漸進(jìn),建議根據(jù)本文順序去封裝一遍,是比較輕松的,當(dāng)然bind還需要判斷是否是new調(diào)用.

完整版本bind

Function.prototype.myBind = function (objThis, ...params) {
  const thisFn = this; // 存儲(chǔ)源函數(shù)以及上方的params(函數(shù)參數(shù))
  // 對(duì)返回的函數(shù) secondParams 二次傳參
  let fToBind = function (...secondParams) {
    console.log('secondParams',secondParams,...secondParams)
    const isNew = this instanceof fToBind // this是否是fToBind的實(shí)例 也就是返回的fToBind是否通過new調(diào)用
    const context = isNew ? this : Object(objThis) // new調(diào)用就綁定到this上,否則就綁定到傳入的objThis上
    return thisFn.call(context, ...params, ...secondParams); // 用call調(diào)用源函數(shù)綁定this的指向并傳遞參數(shù),返回執(zhí)行結(jié)果
  };
  fToBind.prototype = Object.create(thisFn.prototype); // 復(fù)制源函數(shù)的prototype給fToBind
  return fToBind; // 返回拷貝的函數(shù)
};

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論