模拟call,apply, bind 方法
Function.prototype.myCall = function (context, ...params) {
console.log(this, 'call This');
context.func = this;
const res = context.func(...params);
delete context.func;
return res
}
Function.prototype.myCall = function (context, ...params) {
context.func = this;
const res = context.func(...params);
delete context.func;
return res
}
// 模拟 bind 方法
Function.prototype.myBind = function (context, ...params) {
// 保存当前的 this 指向当前的函数
console.log(this, ...params,'this');
const _this = this;
return function(...params2) {
// 由于 bind 方法是返回的一个函数 所以第二个函数也是有可能传参的
// 改变this
return _this.apply(context, [...params, ...params2])
}
}