Function.prototype.myCall = function(context){
if(typeof this !== 'function'){
throw new TypeError('Error')
}
context = context || window
context.fn = this //this为原本的方法
const args = [...arguments].slice(1) //获取传入的参数 去除context
const res = context.fn(args.toString())
delete context.fn
return res
}
//apply类似处理一下数据就好
//bind
Function.prototype.myBind = function(context){
if(typeof this !== 'function'){
throw new TypeError('Error')
}
const _this = this
const args = [...arguments].slice(1) //获取传入的参数 去除context
return function F(){
//如果调用bind是new func.myBind()的形式
if(this instanceof F){
return new _this(...args,...arguments)
}
return _this.apply(context,args.concat(...arguments) )
}
}
console