Function.prototype.myApply = function(objThis,args){
if(typeof this !== 'function'){
throw Error(`需要函数才能绑定`)
}
if(!objThis){
objThis = typeof global === 'object'?global : window
}
let key = Symbol()
objThis[key] = this
let re = objThis[key](...args)
delete objThis[key]
return re
}
var a = {x:200}
function test(a,b,c){
console.log("this.x")
console.log(this.x)
console.log(a)
console.log(b)
console.log(c)
}
var aa = test.myApply(a,[3,99])
//输出-----------
//this.x
//200
//3
//99
javascript手写apply函数(call同理)
最新推荐文章于 2023-05-10 17:03:20 发布