简体   繁体   English

Apply函数如何工作以读取传递给内部函数的所有数组参数?

[英]How does apply function work to read all array arguments passed to inner function?

below i quote from eloquentJavascript 下面我引用eloquentJavascript

Passing along Arguments 传递参数

function noisy(f) {
  return function(arg) {
    console.log("calling with", arg);
    var val = f(arg);
    console.log("called with", arg, "- got", val);
    return val;
  };
}

the above works for single argument and i got that part clear but what i would like to know is the phrase that comes after regarding apply method...it says we can pass an array-like argument list to the above function.... i tried doing it and failed... can anyone illustrate how to pass an array-like object of arguments and the function calling the arguments passed? 以上适用于单个参数,我明白了这一部分,但我想知道的是关于apply方法之后的短语...它说我们可以将类似数组的参数列表传递给上述函数....我尝试这样做并失败了...有人能说明如何传递类似数组的参数对象以及调用所传递参数的函数吗?

i saw the call, apply and bind functions from other sites but i would like to stick on this tutorial and see how it works here? 我看到了来自其他站点的调用,应用和绑定功能,但我想坚持学习本教程,并了解它在这里的工作原理?

i run the above function as: 我将上述功能运行为:

return noisy(String)(0); //works

return noisy(String).apply(null,[0,1,2,3,4]);

gets the same result as above: 得到与上面相同的结果:

calling with 0 called with 0 - got 0

how can i use apply to the noisy function to read all arguments? 我如何使用适用于嘈杂的功能来读取所有参数?

See the bellow snippet 参见波纹管片段

 // Code goes here function noisy(f) { return function() { var args = Array.prototype.slice.call(arguments); for(var i=0;i<args.length;i++){ console.log("calling with", args[i]); var val = f(args[i]); console.log("called with", args[i], "- got", val); } }; } noisy(String)(0); //works noisy(String).apply(null,[0,1,2,3,4]); 

You have to use the arguments object or the ES6 spread operator . 您必须使用arguments对象ES6传播运算符

The second operator of apply expects an array, but it does not apply the array to your function. apply的第二个运算符期望一个数组,但不会将该数组应用于您的函数。 Instead it will call it by adding every array item as a parameter. 相反,它将通过添加每个数组项作为参数来调用它。

暂无
暂无

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

相关问题 args.slice(1)如何分隔传递到PhantomJS和forEach(function(arg,i)…函数的参数? - How does args.slice(1) separate the arguments that are passed into PhantomJS and forEach(function(arg, i)… function to work? 如何获得在JavaScript中传递给函数的函数自变量名称? - How does one get the name of the arguments of a function passed to a function in JavaScript? JS 参数传递给在“return”关键字之后定义的函数。 这是如何运作的? - JS arguments passed on to function defined after "return" keyword. How does this work? 为什么不通过function.apply()递归传递函数参数? - Why are function arguments not getting passed recursively via function.apply()? javascript函数作用域如何与参数一起使用? - How does javascript function scope work with arguments? function arguments如何在下面的promise代码中传递 - How does the function arguments are passed in the following promise code 如何将传递给函数的参数存储在数组中? - How can I store the arguments passed to a function in an array? 您如何将传递给函数的任意数量的参数应用于JavaScript中另一个函数的参数? - How do you apply an arbitrary numbers of arguments passed to function which is argument of another function in JavaScript? 将函数数组传递给另一个 function 作为 arguments 时,如何返回 promise? - How to return a promise when an array of functions is passed, to another function as an arguments? 我将 function (不是谓词)传递给 Array.Filter 并返回给我整个数组 - 它是如何工作的? - I passed a function (not a predicate) to Array.Filter and it returned me the whole array - How does that work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM