首页 > 试题广场 >

改变上下文

[编程题]改变上下文
  • 热度指数:28983 时间限制: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!
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)
【改变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.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)

四种答案,任君选择。

//原生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)
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)

问题信息

难度:
12条回答 11097浏览

热门推荐

通过挑战的用户

查看代码