- call方法的实现是通过this指针的指向不同实现的。
Function.prototype.myCall = function (arg) {
let context = arg || window;
context.fn = this;
const args = [...arguments].slice(1);
const result = context.fn(...args);
delete context.fn;
return result;
}
function test(res) {
console.log(res, ">>>>>>>>>>>")
}
test.myCall(this, 123, 223)
- apply方法的实现,apply方法可以借助call方法进行实现,也可以单独进行实现,apply方法第二个参数传入的是一个数组,这也是和call的区别
Function.prototype.myApply = function (context, arr) {
this.call(context, ...arr)
} // 借助call方法进行实现
Function.prototype.myApply = function (arg, arr) {
let context = arg || window
context.fn = this
let result = context.fn(...arr)
delete context.fn
return result
}
test.myApply(this, [123, 223])
- bind方法的实现,bind方法的话和上边两个方法有区别的是,bind方法不是立即执行的,也就是说不能返回执行结果,应该返回的是一个函数。
Function.prototype.myBind = function (_this, ...args1) {
return (...args2) => {
this.apply(_this, [...args1, ...args2])
}
}
Function.prototype.myBind = function (_this, ...args1) {
return (...args2) => {
let context = _this || window;
context.fn = this;
const result = context.fn(...args1.concat(...args2));
delete context.fn;
return result;
}
}
参考链接: 手动实现call, apply, bind - 简书 (jianshu.com)
(32条消息) js模拟实现call,apply,bind_奋斗在路上-CSDN博客