当前位置:   article > 正文

call ,bind, promise 模仿实现原理_promise bind

promise bind

call 的实现原理

Function.prototype.mycall = function (target){
      target=target || window;
      target.fun = this;
      const arg = [...arguments].slice(1);
      const rut = target.fun(arg);
      delete target.fun;
      retuan rut;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

bind 的实现原理

Function.prototype.mybind = function(content){
    if(typeof this !== "function"){
       return
    }
    content = content || window;
    let args = [...arguments].slice(1)||""
    content.fun = this;
    return (function(args){
       return function(...b){
            let a = [...args,...b]
            content.fun(...a)
       }
    })(args)
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Promise 简单实现原理

class MyPromise{
    constructor(arg){
        this.res;
        this.rej;
        this.callThis();
        this.value = null;
        agr(this.res,this.rej);
    }
    callThis(){
        this.res = this.resolve;
        this.rej = this.resject;
    }
    resolve(success){
       this.value = success;
    }
    resject(error){
        this.value = error;
    }
    then(fun){
       fun(this.value);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/311632
推荐阅读
相关标签
  

闽ICP备14008679号