首页 > 试题广场 >

使用 apply 调用函数

[编程题]使用 apply 调用函数
  • 热度指数:53840 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
实现函数 callIt,调用之后满足如下条件
1、返回的结果为调用 fn 之后的结果
2、fn 的调用参数为 callIt 的第一个参数之后的全部参数
示例1

输入

输出

推荐
因为arguments并非真正的数组,因此要获得callIt的第一个参数之后的所有参数,不能直接使用slice方法截取,需要先将arguments转换为真正的数组才行。有两种常见的方法,一是使用slice方法:var args = Array . prototype . slice . call ( arguments );二是循环遍历逐一填入新数组。在获得了args之后,就可以调用apply来执行传入的函数参数。
function callIt(fn) {
    //将arguments转化为数组后,截取第一个元素之后的所有元素
    var args = Array.prototype.slice.call(arguments,1);
    //调用fn
    var result = fn.apply(null,args);
    return result;
}


编辑于 2015-08-18 21:05:42 回复(23)
function callIt(fn) {
    let argumentsArr = []
    for(let i = 1; i < arguments.length; i++){
        argumentsArr.push(arguments[i])
    }
    return fn.apply(this,argumentsArr)
}


调用apply 注意argument必须是数组形式传参
发表于 2021-04-01 11:09:25 回复(0)
function callIt(fn,...a) {  
    return fn(...a)
}
发表于 2021-03-30 15:58:49 回复(0)
function callIt(...fn) {
    const restParams = fn.slice(1);
    return fn[0](...restParams);
}

发表于 2021-02-20 19:51:52 回复(0)
function callIt(fn) {
   return fn.apply(this,Array.prototype.slice.call(arguments,1)) 
}

发表于 2020-04-09 09:37:24 回复(0)
function callIt(fn) {
    const args = [...arguments]
    const newArgs = args.slice(1)
    return fn.apply(this, newArgs)
}
发表于 2020-03-11 03:51:06 回复(0)
function callIt(fn) {
    return fn.apply(null, Array.prototype.slice.call(arguments, 1));
}

function callIt(fn) {
    return fn.apply(null, [].slice.call(arguments, 1));
}

发表于 2019-08-12 18:57:11 回复(0)
function callIt(fn) {
    var arr = new Array();
    for(var i = 1;i<arguments.length;i++){
        arr.push(arguments[i]);
    }
    return fn.apply(this,arr);
}

发表于 2019-08-10 12:02:12 回复(0)
function callIt(fn) {
    var args=[];
    for(var i in arguments){
        args.push(arguments[i]);
    }
    return fn.apply(null,args.slice(1));
}
发表于 2019-08-06 15:45:07 回复(0)
使用 Array.prototype.slice.call(arguments,1); 获取包含第二个参数以及往后的所有参数。
在函数体内部调用 fn,并用 apply() 方法定义作用于和参数。
apply() 方法和 call() 方法的第一个参数,实际是指明了调用的函数的 this 指向哪个对象。
function callIt(fn) {
    var arr = Array.prototype.slice.call(arguments,1);
    return fn.apply(this,arr);
}
发表于 2017-10-17 18:26:45 回复(0)
如果不懂调用Array.prototype.slice,可用for循环提取arguments除去第一个之后的参数
function callIt(fn) {
    var args = new Array();
    for(var i = 1; i < arguments.length; i++){
        args.push(arguments[i]);
    }
    return fn.apply(null, args);
}
编辑于 2017-07-04 21:57:30 回复(1)
function callIt(fn) {
    return fn.apply(this, [].slice.call(arguments, 1))
}
发表于 2017-03-29 20:38:03 回复(0)
function callIt(fn) {
    var args = Array.prototype.slice.call(arguments, 1);
    return fn.apply(this, args);
}

发表于 2017-03-15 22:28:02 回复(0)
// slice (不改变数组)
function callIt(fn) {
    return fn.apply(this, [].slice.call(arguments, 1));
}

// shift (会改变数组)
function callIt(fn) {
    return [].shift.call(arguments).apply(null, arguments);
}

// ES6语法糖
const callIt = (fn, ...args) => fn(...args); 
编辑于 2017-08-17 22:16:42 回复(3)