简体   繁体   English

Javascript未知数量的参数

[英]Javascript unknown number of arguments

in my project, I register different functions (having different number of arguments) as listeners to a number of events. 在我的项目中,我注册了不同的函数(具有不同数量的参数)作为许多事件的监听器。 When the event takes place, I need to fire the associated function. 当事件发生时,我需要触发相关的功能。 I receive the parameters to be passed to listener method in the form of an array whereas the listener function expect each separate argument. 我以数组的形式接收要传递给listener方法的参数,而listener函数期望每个单独的参数。 So, I am doing it like this but I do not like the approach and would like to know if there is an elegant way of doing it, 所以,我这样做,但我不喜欢这种方法,并想知道是否有一种优雅的方式,

function callListenerWithArgs(func, args){
        switch(args.length){
            case 1:
                func(args[0]);
                break;
            case 2:
                func(args[0], args[1]);
                break;
            case 3:
                func(args[0], args[1], args[2]);
                break;
            case 4:
                func(args[0], args[1], args[2], args[3]);
                break;
            default:
                func();
        }
    }

Use .apply 使用.apply

func.apply(null, args)

If you need to bind to a specific scope, you can pass another argument in to use as this inside the function: 如果您需要绑定到特定的范围,可以在通过另一种说法为使用this功能里面:

func.apply(scope, args);

Also, a nuance of JavaScript is that you can call functions with undefined values. 此外,JavaScript的细微差别在于您可以使用未定义的值调用函数。 So making a small tweak to your existing code will work in 95% of all cases (this isn't suggested as a solution, just pointing it out): 因此,对现有代码进行小幅调整将在95%的情况下工作(这不是建议作为解决方案,只是指出它):

// will handle any number of args up to 7
function callListenerWithArgs(func, args){
    func(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
}

If your func is defined as: 如果您的func定义为:

function foo(a, b, c){
}

you get a , b , c passed in, along with some more undefined values that get ignored. 你得到abc传入,以及一些被忽略的undefined值。 As I said above, this works in 95% of cases. 正如我上面所说,这在95%的情况下起作用。 It doesn't work if you ever check arguments.length in the called function since it will always be the same, regardless of the number of parameters the function defines. 如果您在被调用函数中检查arguments.length ,它将不起作用,因为它始终是相同的,无论函数定义的参数数量是多少。

Try this using function.apply 使用function.apply尝试这个

function callListenerWithArgs(func, args){
    func.apply(window, args);
}

functionName.apply(thisScope, arguments) would be more elegant. functionName.apply(thisScope, arguments)会更优雅。 The arguments argument must be an array. arguments参数必须是数组。

You can build the Array like: 您可以构建数组,如:

var args = [];

switch (arguments.length - 1) {
    case 0:
        break;
    case 1:
        args.push(arguments[1]);
        break;
    case 2:
        args.push(arguments[1], arguments[2]);
        break;
    case 3:
        args.push(arguments[1], arguments[2], arguments[3]);
        break;
    default:
        args = Array.prototype.slice.call(arguments, 1);
}

or if the array is already built, just pass it as the second argument to .apply 或者如果数组已经构建,只需将它作为第二个参数传递给.apply

func.apply(this, args);

看到这里

You can get the number of arguments of a function: 您可以获取函数的参数数量:

var f = function ( ) { console.log(arguments.length); }
f( 2, 3 )  // > 2

That lets you implement your func function directly in place. 这使您可以直接实现您的func功能。

function func() {
  var nbArgs = arguments.length;
  // ...
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM