Function.prototype.myCall=function(context){if(typeofthis!=='function'){// 判断当前调用者是否为函数return}// 保存传入的this指向,这里会出现没有传入this指向的问题,那么that就是默认的window
that = context || window
// 保存当前调用的函数
that.fn =this// 通过slice来截取传入的参数const args =[...arguments].slice(1)// 传入参数调用函数const result = that.fn(...args)// 删除掉fn属性delete that.fn
// 返回结果return result
}
Function.prototype.myApply=function(context){if(typeofthis!=='function'){// 判断当前调用者是否为函数return}// 保存传入的this指向
that = context || window
// 保存当前调用的函数
that.fn =thislet result
// 这里开始判断传入的参数是否存在,此时参数是一个数组形式[thisArg,[传参]]// 那么如果arguments[1]即传参存在的时候,就是需要传参调用保存的函数// 如果不存在就直接调用函数if(arguments[1]){
result = that.fn(...arguments[1])}else{
result = that.fn()}return result
}