简体   繁体   English

您如何将传递给函数的任意数量的参数应用于JavaScript中另一个函数的参数?

[英]How do you apply an arbitrary numbers of arguments passed to function which is argument of another function in JavaScript?

How would you apply a unlimited number of arguments to a function which is a parameter of another function? 您如何将无限数量的参数应用于另一个函数的参数?

This is what I tried: 这是我尝试的:

var callIt = function(fn) {
    for(var i = 1; arguments.length + 1; i++){
        return fn(arguments[i]);
    }
 };

There is a function in the exercise which is used to convey what is being asked: 练习中有一个函数用于传达所要求的内容:

callIt(sumAll, 1);

Maybe I've had too many beers... 也许我喝了太多啤酒...

callIt(sumAll, 2,3) // 5 is expected but I got 2

Make the argument an array , remove the first element, use apply . 参数设置为数组 ,删除第一个元素,然后使用apply

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

Or working from your code: 或使用您的代码进行工作:

var callIt = function(fn) {
    var args = [];
    for(var i = 1; i < arguments.length; i++){
        args.push(arguments[i]);
    }
    return fn.apply(null, args);
};
function show()
        {
            var args = Array.prototype.slice.call(arguments,1);
            arguments[0](args);
        }
 callIt(sumAll, 2,3) // 5 is expected but I got 2 

javascript at Question return s from within for loop at first iteration. 第一次迭代时, for循环中的Question return s中的javascript Also, for loop does not have a condition which would return false . 另外, for循环没有返回falsecondition You can utilize rest parameter to separate function parameter from remainder of parameters passed to callIt 您可以利用rest parameter将函数参数与传递给callIt rest parameter分开

 var callIt = function(fn, ...args) { var res = 0; for(var i = 0; i < args.length; i++){ res += fn(args[i]); } return res }; var result = callIt(function(n) { return n }, 2,3); console.log(result); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaScript中的函数(本身就是一个参数)中传递参数 - How argument is being passed in the function(which itself is an argument) in JavaScript 你如何咖喱任意数量的任何javascript函数? - How do you curry any javascript function of arbitrary arity? 将 function 作为参数传递给另一个 function 并将其应用于传入的元素 - Passing in a function as an argument to another function and apply it to the passed in element 您如何部分地应用带有多个参数的函数? - How do you partially apply a function with multiple arguments? 作为参数传递的函数的访问参数 - Access arguments of a function passed as argument 用Javascript调用任意函数“应用” - Calling arbitrary function with Javascript 'apply' Javascript:如何填充传递给函数的参数? - Javascript: How to populate arguments passed to a function? 如何确定一个函数的参数是否作为参数传递给另一个函数 - how to determine if the argument of a function passed into another function as an argument is undefined or not 我们如何将参数传递给 function,它本身在 javascript 中作为参数传递 - How can we pass argument to function which itself is passed as argument in javascript 在CoffeeScript中,如何将函数作为函数的参数传递,而函数又需要一个参数? - In CoffeeScript, how do you pass a function as an argument of a function, which also in turn takes an argument?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM