首页 > 试题广场 >

修改 this 指向

[编程题]修改 this 指向
  • 热度指数:117335 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
封装函数 f,使 f 的 this 指向指定的对象
示例1

输入

输出

推荐
function bindThis(func, oTarget) {
    return function(){
        return func.apply(oTarget, arguments);
    };
}

function bindThis(f, oTarget) {
return  f.bind(oTarget);
}
两种方法都可以
编辑于 2016-03-23 10:54:52 回复(17)
手写Bind
function bindThis(f, oTarget) {
    return function(...args) {
        oTarget.fn = f
        var result = oTarget.fn(...args)
        delete oTarget.fn
        return result
    }
}
编辑于 2021-07-01 23:16:04 回复(1)
function bindThis(f, oTarget) {
    return f.bind?f.bind(oTarget):function(){return f.apply(oTarget,arguments)}
}
如果有bind函数直接调用,没有调用call和apply函数进行绑定,因为立即调用缘故采用匿名函数封装,call写法:
function bindThis(f, oTarget) {
    return f.bind?f.bind(oTarget):function(){return f.apply(oTarget,..arguments)}
}

发表于 2020-10-25 22:00:41 回复(0)

function bindThis(f, oTarget) {

return function(){

return f.apply(oTarget,arguments);

}

}

发表于 2020-02-28 23:38:35 回复(0)
function bindThis(f, oTarget) {
    if(f.bind){
        return f.bind(oTarget);
    } else {
        return function(){
            return f.apply(oTarget,arguments);
        };
    }
}

发表于 2016-05-31 00:59:37 回复(4)
function bindThis (func, context) {
    return function () {
        return func.apply(context, arguments)
    }
}

编辑于 2016-08-15 10:05:26 回复(8)
function bindThis(f, oTarget) {
    return function(){
        return f.apply(oTarget,arguments);
    }
}
为什么有匿名函数,是因为apply call 是绑定之后是立即调用的,所以需要匿名函数包装且需要传入原函数的参数argumengts. bind 会创建一个新函数,即函数副本,绑定函数运行时本身的参数按照顺序作为原函数的参数来调用原函数。
发表于 2018-07-20 14:09:01 回复(1)
知识点:this指向、arguments类数组对象、柯里化

要做两件事
[todo1]
使f的this指向指定对象函数调用时,使其执行上下文为对象使对象将函数作为其方法调用
[todo2]
对象在调用函数时,需要传入函数所需的形参
// 声明
function callback(arg1, arg2){}
function Func(callback){
    return function(){
        // 在该层封装函数中使用arguments对象取callback形参
    }
}
// 调用
Func(callback)('形参1','形参2')

实现:
// 方法一:apply
function bindThis(f, oTarget) {
    return function() {
        let args = [].slice.call(arguments)
        return f.apply(oTarget,args)
    }   
}
// 方法二:call
function bindThis(f, oTarget) {
    return function() {
        let args = [].slice.call(arguments)
        return f.call(oTarget,...args)
    }   
}
// 方法三:bind
function bindThis(f, oTarget) {
    return f.bind(oTarget)
}
// 方法四:将函数作为对象的方法调用
function bindThis(f, oTarget) {
    // 使用call、apply、bind方法时,该函数是添加到对象原型上的
    // oTarget.__proto__.fn = f
    oTarget.fn = f
    return function(){
        let args = [].slice.call(arguments)
        return oTarget.fn(...args)
    }
}










发表于 2020-08-01 19:25:33 回复(2)
看了11天终于看懂题目了,后面的(2,3)意思就是:需要前面返回一个函数从而传参(2,3)来执行该函数,
所以多一层return ,这样里面的arguments就能拿到(2,3)
编辑于 2017-05-24 09:37:26 回复(2)
function bindThis(f, oTarget) {
    //方法1 apply()
    //return function(){
    //    return f.apply(oTarget,arguments);
    //};
    //方法2 bind()
    //return f.bind(oTarget);
    //方法3 call()
    return function(x,y){
        return f.call(oTarget,x,y);
    };
}

发表于 2018-09-25 21:52:41 回复(2)
call,apply,bind 都是改变上下文的,但是call apply是立即执行的,而bind是返回一个改变上下文的函数副本

发表于 2019-06-13 11:18:18 回复(0)
function bindThis(f, oTarget) {
    return f.bind(oTarget);
}
发表于 2015-09-07 16:01:41 回复(0)
return f.bind(oTarget);
发表于 2017-03-22 22:42:58 回复(0)
function bindThis(f, oTarget) {
    return f.bind(oTarget);
}
链接:https://www.nowcoder.com/questionTerminal/a616b3de81b948fda9a92db7e86bd171
来源:牛客网
修改this指针有三种方法:
  bind apply call
其中 apply和call都是立即执行的,只有bind是将修改this指针后,返回一个新的函数,不会立即调用
apply和call的区别主要是,apply只接收数组形式的参数输入 

发表于 2021-03-29 23:15:58 回复(0)
function bindThis(f, oTarget) {
    var result = function (x,y) {
        return f.call(oTarget,x,y);
    };
    return result;
}

发表于 2015-10-14 10:19:15 回复(1)
知识点:call()、bind()、apply()三种方法,用于改变this指向;
function bindThis(f, oTarget) {
    return function() {
        return f.call(oTarget,...arguments);
    }
}
function bindThis(f, oTarget) {
        return f.bind(oTarget);
}
function bindThis(f, oTarget) {
    return function() {
        return f.apply(oTarget, arguments);
    }
}




发表于 2022-03-26 16:50:58 回复(1)
function bindThis(f, oTarget,...args) {
    return function(...newArg){
        return f.apply(oTarget,args.concat(newArg))
}
}
发表于 2021-08-06 11:27:08 回复(0)
第一种:function bindThis(f, oTarget) {
return function(){
return f.call(oTarget,...arguments)
}
} //使用call方法 注意arguments对象的参数形式
第二种:function bindThis(f, oTarget) {
return function(){
return f.apply(oTarget,arguments)
}
} //使用apply方法
第三种:function bindThis(f, oTarget) {
return f.bind(oTarget)
}// bind方法 直接绑定this指向
考察点:call,apply和bind的作用和区别?
相同点:1:都能改变this指向
2:都能传递参数
3:都能通过方法"."方法名调用
不同点:1:函数名不同
  1. 参数传递方式不同(注意看第二个)
  2. 改变this指向的时机不同(bind在复制时改变,其他两个在调用时改变)
  3. 参数传递时机不同(注意看第三个)
发表于 2021-03-04 10:54:38 回复(1)
bind()方法主要就是将函数绑定到某个对象,bind()会创建一个函数,函数体内的this对象的值会被绑定到传入bind()第一个参数的值,例如,f.bind(obj),实际上可以理解为obj.f(),这时,f函数体内的this自然指向的是obj
发表于 2021-01-22 14:28:09 回复(0)
// apply
function bindThis(f, oTarget) {
  return function() {
    return f.apply(oTarget, arguments);
  }
}

// call
function bindThis(f, oTarget) {
  return function() {
    return f.call(oTarget, ...arguments);
  }
}

// bind
function bindThis(f, oTarget) {
  return f.bind(oTarget);
}
没有提词器不觉得很扯吗?

发表于 2021-01-06 11:38:19 回复(0)
bindThis(f,oTarget){
    return f.bind(oTarget);
}
bindThis(f,oTarget){
    return function(){
        return f.apply(oTarget,arguments);
    }
}
bindThis(f,oTarget){
    return function(x,y){
        return f.call(oTarget,x,y);
    }
}
发表于 2016-03-31 09:52:50 回复(0)