核心提示:前端挑战之js编程题题目要求:封装函数 f ,使 f 的 this 指向指定的对象思路:看到题目要求,首先应该想到js 中改变this 指向的三个方法 (bind , apply ,call),这样就...
前端挑战之js编程题
题目要求:
封装函数 f ,使 f 的 this 指向指定的对象
思路:
看到题目要求,首先应该想到js 中改变this 指向的三个方法 (bind , apply ,call),这样就能够解决该问题了。
方案1:
function bindThis(f,oTarget){
return function (){
return f.apply(oTarget,arguments);
};
}
方案2:
function bindThis(f,oTarget){
return f.bind(oTarget);
}
方案3:
function bindThis(f, oTarget) {
return function(x,y){
return f.call(oTarget,x,y);
};
}