自定义bind方法:bind做了什么事
1、传入参数
2、返回一个新函数
3、改变this指向
Function.prototype.myBind=function(target){
const _this=this;
let arr1 = [];
for(let i = 1; i<arguments.length;i++){
arr1.push(arguments[i]);
}
//实例对象调用myBind方法时,返回一个新的函数
return function(){
let arr2 = [];
for(let i = 0; i<arguments.length;i++){
arr2.push(arguments[i]);
}
target.__proto__.fn=_this;
target.fn(...arr1,...arr2)
delete target.__proto__.fn;
}
}
function login(a,b,c){
console.log(this);
console.log(a,b,c)
}
const obj = {
name:"lucy"
}
const newFn=login.myBind(obj,1,2);
newFn(3);