JavaScript 深入之 call 和 apply 的模拟实现
1 | // 第一版 |
但是第一版不可以传递多个参数
1 | // 第二版 |
第二版的问题是,1.this 参数可以传 null,当为 null 的时候,视为指向 window2.函数是可以有返回值的!
1 | // 第三版 |
es6 版
1 | Function.prototype.call2 = function (context, ...args) { |
apply 的实现跟 call 类似,在这里直接给代码,代码来自于知乎 @郑航的实现:
1 | Function.prototype.apply = function (context, arr) { |