题解 | #修改 this 指向#

修改 this 指向

https://www.nowcoder.com/practice/a616b3de81b948fda9a92db7e86bd171

function bindThis(f, oTarget) {
    //封装函数 f,使 f 的 this 指向指定的对象.
    //记得封装 return function(){}
    
    //可以用call()、apply()、bind(),其中arguments为参数列表
    return function(){
        return f.apply(oTarget, arguments);    //需把局部变量arguments参数传到函数中,so不可省。apply的第二个参数是数组。
    }
    
    return function(){
        return f.call(oTarget, ...arguments);        
    }/**/
    
    return f.bind(oTarget);        //本身返回的是一个函数,so 参数可省。
}

全部评论

相关推荐

头像
04-29 10:53
已编辑
东北大学 自动化类
点赞 评论 收藏
转发
1 收藏 评论
分享
牛客网
牛客企业服务