# 题目描述

请你自定义函数实现Function.apply方法

# 分析

call方法类似,只是apply

只有两个参数 👇

apply(thisArg, argsArray)

  • 第一个是修改的this
  • 第二个是参数数组

# 代码实现

Function.prototype.myApply = function(context){
  context = context || window;
  context.fn = this;
  const args = arguments[1];
  console.log("args: ", args);
  const res = context.fn(...args);
  delete context.fn;
  return res;
}

console.log("myApply");
test.myApply({a : "我是aa", b : "我是bb"}, [1, 2])

console.log("原生apply");
test.apply({a : "我是aa", b : "我是bb"}, [1, 2])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

测试效果如下

1