function Person(){}
Person.call_=function (obj){
let s=Symbol();
obj[s]=this;
let result=obj[s](...[].slice.call(arguments,1));//或...Array.from(arguments).slice(1)
delete obj[s];
return result;
}
Person.apply_=function (obj,arr){
if (!arr[Symbol.iterator])
throw new Error("传入了一个非迭代参数");
let s=Symbol();
obj[s]=this;
let result=obj[s](...arr);
delete obj[s];
return result;
}
Person.bind_=function (obj){
if (!obj){
throw new Error("缺少obj");
}
let self=this;
let args=[].slice.call(arguments,1);//或Array.from(arguments).slice(1)
return function (){
return self.apply(obj,args.concat([].slice.call(arguments)));
}
}
关键点:
1.函数.方法[call | apply | bind],这里把函数看做对象,所以方法内this指向函数
2.通过把函数挂到目标对象下再进行访问,就可以实现this指向的改变
3.为了防止命名冲突,这里使用Symbol符号的方式保证唯一性地调用函数;在调用完后应删除
4.[].slice.call(arguments,1) 是因为arguments是类数组对象(类似的还有DOM节点),没有数组的方法(pop,push等)。数组的方法内部是通过this获取并修改原数组,所以我们通过call改变this指向就达到模拟在arguments上调用slice方法的效果。当然也可以使用Array.from()将类数组或可迭代对象转为数组后调用slice,不过性能上稍微差一丢丢