手撕call、apply、bind

 

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;

  }

}

 

posted @ 2022-08-22 13:33  浪浪浪浪浪浪浪浪  阅读(19)  评论(0编辑  收藏  举报