手写bind,apply,call方法

//1.定义myBind方法
Function.prototye.mybind=function(thisArg,...args){
  //2.返回绑定this的新函数
  return(...reArgs)=>{
  	return this.call(thisArg,...args,...reArgs)
  }
}

//1.定义myapply方法
Function.prototype.myapply=function(thisArg,args){
	//2.设置this并调用原函数
  const key=Symbol('key')
  thisArg[key]=this
  delete thisArg[key]
  //3.接收剩余参数并返回结果
  const res=thisArgs[key](...args)
  return this
}

//跟apply方法基本相同
//1.定义mycall
Function.prototype.mycall=function(thisArgs,...args){
//2.设置this并调用原函数
  const key=Symbol('key')
  thisArg[key]=this
  delete thisArg[key]
  //3.接收剩余参数并返回结果
  const res=thisArg[key](...args)
  return res
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务