Function.prototype.myBind = function () {
//将参数拆解为数组
const args = Array.prototype.slice.call(arguments)
//获取this(数组的第一项)
const t = args.shift()
//fn1.bind(...)中的fn1
const self = this
//返回一个函数
return function (){
return self.apply(t,args)
}
}
function fn1(a,b,c){
console.log(a,b,c,this)
return 'this is fn1'
}
const fn2 = fn1.myBind({x:888},10,20,30)
const res = fn2()
console.log(res)//打印:10,20,30,{x:888}
前端面试必问题型:手写bind
最新推荐文章于 2024-07-11 17:17:41 发布