首页 > 其他分享 >手撕call、apply、bind

手撕call、apply、bind

时间:2022-08-22 13:46:59浏览次数:92  
标签:... const target symbolKey bind call arguments res apply

 

Funtion.prototype.call_ = (target, ...arguments) => {

   target = target || window;
   const symbolKey = Symbol();
   target[symbolKey] = this;
   const res = target[symbolKey](...arguments);
   delete target[symbolKey];

  return res;

}

 

Function.prototype.apply_ = (target, arguments) => {

  target = target || window;

  const symbolKey = Symbol();

  target[symbolKey] = this;

  const res = target[symbolKey](...arguments);

  delete target[symbolKey];

  return res;

}

 

Function.prototype.bind_ = (target, arguments) => {

  target = target || {};

  const symbolKey = Symbol();

  return function(rest) {

    target[symbolKey] = this;

    const res = target[symbolKey](...argyments, ...rest);

    delete target[symbolKey];

    return res;

  }

}

 

标签:...,const,target,symbolKey,bind,call,arguments,res,apply
From: https://www.cnblogs.com/hamili/p/16612537.html

相关文章