• call

      1. Function.prototype.myCall = function (obj, ...arg) {
      2. const context = obj || window;
      3. const fn = Symbol(); // 保证唯一
      4. context[fn] = this;
      5. const res = context[fn](...arg);
      6. delete context[fn];
      7. return res;
      8. }
    • apply

      1. Function.prototype.myApply = function (obj, arg) {
      2. const context = obj || window;
      3. const fn = Symbol(); // 保证唯一
      4. context[fn] = this;
      5. const res = context[fn](...arg);
      6. delete context[fn];
      7. return res;
      8. }
    • bind ```javascript Function.prototype.bind = function (obj, …args) { const context = obj || window; const fn = Symbol() context[fn] = this; let that = this;

      const res = function(…others){

      1. return that.apply(
      2. this instanceof that ? this : context, //new时, this指向res的实例,res继承自that
      3. [...args,...others]
      4. )

      }

      // 如果绑定的是构造函数 那么需要继承构造函数原型属性和方法 res.prototype = Object.create(that.prototype)

      return res }

    // 使用 function Point(x, y) { this.x = x; this.y = y; }

    Point.prototype.toString = function () { return this.x + ‘,’ + this.y; }

    let YPoint = Point.bind(null, 1); let axiosPoint = new YPoint(2);

    console.log(axiosPoint.toString()) // ‘1,2’ ```