function test1(a,b,c){
console.log('this',a,b,c,this);
}
Function.prototype.myBind = function(){
// 拿到传进来的参数
let arr = [...arguments]
// obj就是要指向的对象
let obj = arr.shift()
return ()=>{
return this.myApply(obj,arr)
}
}
Function.prototype.myApply = function (obj,arr){
if (typeof obj === 'undefined' || obj === null) {
obj = window;
}
obj = Object(obj);
const keyFn = Symbol('__myKeyFn__');
obj[keyFn] = this;
const reasult = obj[keyFn](...arr);
// 调用以后删除
delete obj[keyFn];
return reasult;
}
const bindTest = test1.myBind({name:'大锤'},1,5,99)
bindTest()
最后控制台输出