Function.prototype.myCall=function(ctx,...args){
const _ctx = (ctx===null||ctx===undefined)?globalThis:Object(ctx)
const key = Symbol('key')
Object.defineProperty(_ctx,key,{value:this,enumerable:false})
const result= _ctx[key](...args)
delete _ctx[key]
return result
}
``
Function.prototype.myApply=function(ctx,args){
const _ctx=(ctx===null||ctx===undefined)?globalThis:Object(ctx)
const key=Symbol('key')
Object.defineProperty(_ctx,key,{value:this,enumerable:false})
const result = _ctx[key](...args)
delete _ctx[key]
return result
}
手写call,apply
最新推荐文章于 2024-09-05 16:27:03 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)