开发者社区 问答 正文

手写apply函数?

展开
收起
前端问答 2019-11-24 20:12:36 781 分享
分享
版权
举报
1 条回答
写回答
取消 提交回答
  • 前端问答小助手
    Function.prototype.myApply = function(context) {
      if (typeof this !== 'function') {
        throw new TypeError('Error')
      }
      context = context || window
      context.fn = this
      let result
      // 处理参数和 call 有区别
      if (arguments[1]) {
        result = context.fn(...arguments[1])
      } else {
        result = context.fn()
      }
      delete context.fn
      return result
    }
    
    2019-12-17 07:44:32 举报
    赞同 1 评论

    评论

    全部评论 (0)

    登录后可评论
问答分类:
问答地址:
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等