Function.prototype.myCall=function (context){
if(typeof this != 'function'){
throw new TypeError('not a function')
}
context = context || window
const args=[...arguments].slice(1)
context.fn=this
const res = context.fn(...args)
delete context.fn
return res
}
//使用
function ge(name){
this.name=name
}
function agc(){
this.name='abcdadf'
}
let s=new agc()
ge.myCall(s,'abxcf')
console.log(s)
//agc { name: 'abxcf' }
手动实现call的功能
最新推荐文章于 2022-03-22 10:23:08 发布