首页 > 试题广场 >

改变上下文

[编程题]改变上下文
  • 热度指数:28806 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
将函数 fn 的执行上下文改为 obj,返回 fn 执行后的值
示例1

输入

alterContext(function() {return this.greeting + ', ' + this.name + '!'; }, {name: 'Rebecca', greeting: 'Yo' })

输出

Yo, Rebecca!
【改变this指向问题】 function alterContext(fn,obj){ return fn.call(obj,obj.name,obj.greeting); }
发表于 2022-04-11 13:02:38 回复(0)
function alterContext(fn, obj) {
    return fn.apply(obj);
}

发表于 2020-03-26 14:00:34 回复(0)
function alterContext(fn, obj) {
	return fn.apply(obj,obj);
}

测试通过!

发表于 2017-04-10 17:27:18 回复(0)
主要有三种答案。
function alterContext(fn, obj) {
  return fn.bind(obj)();//.bind()返回的是一个函数,所以需要立即执行。 }

function alterContext(fn, obj) {
  return fn.call(obj);
}

function alterContext(fn, obj) {
  return fn.apply(obj);
}

编辑于 2016-08-29 18:55:31 回复(3)
在JavaScript中,函数是一种对象,其上下文是可以变化的,对应的,函数内的this也是可以变化的,函数可以作为一个对象的方法,也可以同时作为另一个对象的方法,可以通过Function对象中的call或者apply方法来修改函数的上下文,函数中的this指针将被替换为call或者apply的第一个参数。将函数 fn 的执行上下文改为 obj 对象,只需要将obj作为call或者apply的第一个参数传入即可。
function alterContext(fn, obj) {
  return fn.call(obj,obj);
 }

编辑于 2015-08-19 10:41:51 回复(8)
共有四种正确方案(按提交的运行时间由快到慢排列):
1. apply 方法改变函数 this 值(平均 172ms )
function alterContext(fn, obj) {
  returnfn.apply(obj);
}
2. call 方法改变函数 this 值(178 ms)
function alterContext(fn, obj) {
  returnfn.call(obj);
}
3. bind 方法创建指定 this 值的函数实例(208 ms)
function alterContext(fn, obj) {
  constbindedFn = fn.bind(obj);
  returnbindedFn();
}
4. 给 obj 增加 fn 方法(213 ms)
function alterContext(fn, obj) {
    obj.fn = fn;
  return obj.fn();
}

发表于 2018-07-13 17:14:35 回复(5)

四种答案,任君选择。

//原生js法:
function alterContext(fn, obj) {
    obj.temp = fn;
    let result = obj.temp();
    delete obj.temp;
    return result
}
//另外三种es6新增的方法
function alterContext(fn, obj) {
  return fn.bind(obj)();//.bind()返回的是一个函数,所以需要立即执行。 }

function alterContext(fn, obj) {
  return fn.call(obj);
}

function alterContext(fn, obj) {
  return fn.apply(obj);
}
发表于 2021-09-14 14:33:16 回复(0)
可通过给对象创建并赋值新方法 fn() 来实现:
function alterContext(fn, obj) {
    obj.func = fn;
    return obj.func();
}
发表于 2017-06-30 22:17:27 回复(1)
function alterContext(fn, obj) {
    var args = Array.prototype.slice.apply(arguments);
    return fn.apply(obj, args);
}

发表于 2017-04-16 09:24:57 回复(0)
我想问一下 为什么return fn.apply(obj,obj);可以 return fn.apply(this,obj);不行呢?
发表于 2018-05-06 21:19:57 回复(0)
function alterContext(fn, obj) {
        return fn.call(obj)
}
发表于 2023-06-05 10:43:15 回复(0)
function alterContext(fn, obj) {
    var obj={
        name: 'Rebecca', 
        greeting: 'Yo',
        alterContext:function(){
           return this.greeting + ', ' + this.name + '!'; 
        }
    }
不知道写了个啥,但是运行通过了
    return obj.alterContext()
}
发表于 2023-06-04 10:55:46 回复(0)
function alterContext(fn, obj) {
    return fn.bind(obj)()
}
call和apply会立即执行,bind不会
发表于 2023-05-13 16:35:41 回复(0)

function alterContext(fn, obj) {
    return fn.apply(obj);
}

发表于 2022-07-23 18:22:58 回复(0)
function alterContext(fn, obj) {
    // return fn.call(obj);
    return fn.apply(obj);
}

发表于 2022-02-16 09:40:26 回复(0)
function alterContext(fn, obj) {
    return fn.bind(obj)();//.bind()返回的是一个函数,所以需要立即执行。 }
    return fn.apply(obj);
    return fn.call(obj);
}

发表于 2022-01-07 11:35:37 回复(1)
function alterContext(fn, obj) {
    obj.fn = fn
    return obj.fn()
}
发表于 2022-01-05 16:56:40 回复(0)
不懂什么叫上下文😭😭😭我还有救吗
谁能给我解释一下,多些例子
发表于 2021-09-14 21:49:44 回复(0)
if(!Function.prototype.call) {
        Function.prototype.call = function(ctx) {
            var args = []
            ctx.fn = this
            for (var i = 1; i < arguments.length; i++) {
                args.push('arguments['+ i +']')
            }
            var result = eval('ctx.fn('+ args +')')
        }
    }
    
    if(!Function.prototype.apply) {
        Function.prototype.apply = function(ctx) {
            var args = []
            ctx.fn = this
            for (var 0 = 1; i < arguments[1].length; i++) {
                args.push('arguments['+ i +']')
            }
            var result = eval('ctx.fn('+ args +')')
        }
    }
    
    if(!Function.prototype.bind) {
        Function.prototype.bind = function(ctx) {
            var fTobind = this
            var slice = Array.prototype.slice
            var baseArgs = slice.call(arguments, 1)
            var fBound = function() {
                return fTobind.apply(this instance of fBound ? this : ctx, baseArgs.concat(slice(arguments)))
            }
                
            fBound.prototype = Object.create(fTobind.prototype)
            fBound.prototype.conctructor = fBound
            
            return fBound
        }
    }

发表于 2021-09-13 14:34:29 回复(0)
这题目重复了吧
发表于 2021-07-25 16:10:16 回复(0)