Javascript:“无限”函数参数? [英] Javascript: "Infinite" parameters for function?

查看:126
本文介绍了Javascript:“无限”函数参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Chrome中,当我在 console.log 中输入

时:

  console.log(参数,参数,参数,参数,参数,参数,参数,参数,A参数,参数,参数,参数,参数); 

...它正确打印,没有错误或警告。

  console.log(A parameter,A parameter ,参数,参数,参数,参数,参数,参数,参数,参数,参数,参数 ,参数,参数,参数,参数,参数,参数,参数,参数,参数,参数 ,参数,参数,参数,参数,参数,参数,参数,参数,参数 

如何获取无限参数函数?

解决方案

函数可以访问一个名为 arguments 的类似数组的对象,该对象包含它们收到的所有参数

 函数print_my_arguments(/ ** /){
var args = arguments;
for(var i = 0; i< args.length; i ++){
console.log(args [i]);
}
};

你可以用apply方法进行相反的转换(给出一个参数列表) :

  //这些是等价的:
print_my_arguments(1,2,3);
print_my_arguments.apply(null,[1,2,3]);

//'apply`的第一个参数是`this`。
//当函数是方法时使用它。
foo.bar(1,2,3);
var f = foo.bar; f.apply(foo,[1,2,3]);

一些重要的注意事项:


  1. 参数不是实际的数组,它没有任何常用的数组方法(slice,join等)。您可以使用以下行将其转换为数组:

      var args = Array.prototype.slice.call(arguments); 


  2. 如果您希望数组仅包含非指定参数, :

      function foo(first_arg,second_arg / ** /){
    var variadic_args = Array.prototype。 slice.call(arguments,2);
    }

  3. 不是每个浏览器都可以处理任意数量的函数参数。上次我测试了这个,在Chrome和IE中,有大约200.000个参数之后有一个stackoverflow。如果你的函数可以接收到任意数量的参数,那么可以考虑将所有这些参数放在一个常规数组中。 那些 / ** / 出现在我的示例的参数列表中的注释不是强制性的。它们只是一种编码惯例,我用它来标记我的可变参数函数并将它们与常规函数区分开来。

      //快速一目了然会提示该函数没有收到
    //参数,但实际上它是一个可变参数函数,它通过`arguments`对象获取
    //其参数。
    函数foo(){
    console.log(arguments.length);
    }



In Chrome, when I type console.log in the one below:

console.log("A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter");

...it prints it correctly, with no errors or warnings. I appended more parameters, but it still prints it out correctly.

console.log("A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter");

How can I have an "infinite" parameter function just like that one?

解决方案

Functions can access an array-like object called arguments that contains all the arguments that they received

function print_my_arguments(/**/){
    var args = arguments;
    for(var i=0; i<args.length; i++){
        console.log(args[i]);
    }
};

And you can do the opposite conversion (call a function given a list of arguments) with the apply method:

// These are equivalent:
print_my_arguments(1,2,3);
print_my_arguments.apply(null, [1,2,3]);

// The first parameter to `apply` is the `this`.
// It is used when the function is a method.
foo.bar(1,2,3);
var f = foo.bar; f.apply(foo, [1,2,3]);

Some important points to note:

  1. arguments isn't an actual array and it has none of the usual array methods (slice, join, etc). You can convert it to an array with the following line:

    var args = Array.prototype.slice.call(arguments);
    

  2. Slice is also useful if you want your array to only contain the non-named arguments that were received:

    function foo(first_arg, second_arg /**/){
        var variadic_args = Array.prototype.slice.call(arguments, 2);
    }
    

  3. Not every browser can handle an arbitrarily large number of function parameters. Last time I tested this, in Chrome and IE there was a stackoverflow after some 200.000 arguments. If your function can receive an arbitrarily large number of arguments, consider packing all of those arguments in an regular array instead.

  4. Those /**/ comments that appear in the arguments lists for my examples are not mandatory. They are just a coding a convention that I use to mark my variadic functions and differentiate them from regular functions.

    // A quick glance would suggest that this function receives no
    // parameters but actually it is a variadic function that gets
    // its parameters via the `arguments` object.
    function foo(){
        console.log(arguments.length);
    }
    

这篇关于Javascript:“无限”函数参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆