Open nmsn opened 1 year ago
Function.prototype.myApply = function(obj, args) {
const context = obj || window;
context.fn = this;
cosnt result = context.fn(...args);
delete context.fn;
return result;
}
Function.prototype.newCall = function(obj, ...args) {
const context = obj || window;
context.fn = this;
cosnt result = context.fn(...args);
delete context.fn;
return result;
}
Function.prototype.myBind = function(obj, ...args) {
const fn = this;
// 真正返回的函数
return function exec(...args2) {
return fn.apply(
// 重点就在于这句话
// 前半句判断是返回的函数当作实例方法执行时
// 后半句为传入的绑定对象
this instanceof exec ? this : obj,
[...args,...args2]
)
}
}
如题