参数的Javascript数目不详 [英] Javascript unknown number of arguments

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

问题描述

在我的项目,我注册了不同的功能(具有不同数量的参数),作为听众的一些事件。当事件发生时,我需要触发相关的功能。我收到的参数要传递给侦听器方法在数组的形式,而听者功能指望每个单独的说法。所以,我做它像这样,但我不喜欢这种方法,并想知道是否有这样做的一个优雅的方式,

 函数callListenerWithArgs(FUNC,参数){
        开关(args.length){
            情况1:
                FUNC(参数[0]);
                打破;
            案例2:
                FUNC(参数[0],ARGS [1]);
                打破;
            案例3:
                FUNC(参数[0],ARGS [1],ARGS [2]);
                打破;
            情况4:
                FUNC(参数[0],ARGS [1],ARGS [2],ARGS [3]);
                打破;
            默认:
                FUNC();
        }
    }


解决方案

使用。适用

  func.apply(NULL,参数)

如果您需要绑定到特定的范围,可以在通过另一种说法为使用该里面的功能:

  func.apply(范围参数);

此外,JavaScript的一个细微差别是,你可以调用函数未定义值。因此,作出小的调整到您现有的code将在所有情况下,95%的工作(这不建议作为一种解决方案,只是指出出来):

  //将处理任何数量的参数的个数达7
功能callListenerWithArgs(FUNC,参数){
    FUNC(参数[0],ARGS [1],ARGS [2],ARGS [3],ARGS [4],ARGS [5],ARGS [6]);
}

如果你的 FUNC 的定义是:

 函数foo(A,B,C){
}

A B C 传递在,随着该被忽略一些未定义值。正如我前面所说,这部作品在95%的病例。如果你检查与arguments.length 中调用的函数,因为它永远是相同的,无论参数的函数定义了一些它不能正常工作。

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. 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

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:

func.apply(scope, args);

Also, a nuance of JavaScript is that you can call functions with undefined values. 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):

// 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:

function foo(a, b, c){
}

you get a, b, c passed in, along with some more undefined values that get ignored. As I said above, this works in 95% of cases. 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.

这篇关于参数的Javascript数目不详的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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