call

文章链接

      Function.prototype.myCall = function (context) {
        if (typeof this !== 'function') {
          throw new TypeError('Function.prototype.myCall - what is trying to be bound is not callable')
        }

        context = context || window
        context._fn = this
        var args = Array.prototype.slice.call(arguments, 1)
        var result = context._fn(...args)
        delete context._fn
        return result
      }
    
      function fn1(age, car) {
        console.log(this.name)
        console.log(age)
        console.log(car)
      }

      const obj = {
        name: '雪月',
      }

      fn1.myCall(obj, 18, '保时捷')