可变长度参数的Javascript约定

Ada*_*dam 9 javascript coding-style

我正在进行更多的javascript开发,并希望确保我遵循流行的约定.目前我有一个库,它包含可以通过1个模型进行操作的函数,或者许多模型.

鉴于一些javascript库非常受欢迎的气候,我很好奇; 我是通过枚举参数变量,还是允许其中一个参数成为数组来实现我的'单项或列表'要求,从而符合'事实标准'?

场景1:参数枚举

// passing a single entity to my function
sendMail( email, recipient1 );

// passing multiple entities to my function
sendMail( email, recipient1, recipient2 );
Run Code Online (Sandbox Code Playgroud)

场景2:实体参数是单个实例或数组

// pass a single entity
sendMail( email, recipient1 );

// passing multiple entities
sendMail( email, [recipient1, recipient2] );
Run Code Online (Sandbox Code Playgroud)

我已经看到了使用"场景2"的jQuery领域,但我还是想问一下 - 哪种方法最受欢迎,为什么?

谢谢

[编辑]

一些注释遵循相同的方式,使用一个参数对象 - 类似于'场景2' - 但我觉得它引入了不必要的复杂性 - 元素不需要命名,因为它们只是一个可变长度列表.我想我会在这里添加,以防我的问题不够明确.

[编辑]

我通过jQuery-1-7.js看到这样的代码

queue: function( elem, type, data ) {
    var q;
    if ( elem ) {
        type = ( type || "fx" ) + "queue";
        q = jQuery._data( elem, type );

        // Speed up dequeue by getting out quickly if this is just a lookup
        if ( data ) {
            if ( !q || jQuery.isArray(data) ) {
                q = jQuery._data( elem, type, jQuery.makeArray(data) );
            } else {
                q.push( data );
            }
        }
        return q || [];
    }
}
Run Code Online (Sandbox Code Playgroud)

[编辑]

在与JP讨论之后,我想出了这个 - 我不是说这是正确的选择,但它非常灵活......

lastArgumentAsParams: function()
{
    var callerArgs = jQuery.makeArray(this.lastArgumentAsParams.caller.arguments);

    // return empty set if caller has no arguments
    if ( callerArgs.length == 0 )
        return [];
     callerArgs.splice(0, callerArgs.length - 1)
    // remove all but the last argument
    if ( callerArgs.length == 1 && jQuery.isArray(callerArgs[0]))
        return callerArgs[0];
    else
        return callerArgs;
}
Run Code Online (Sandbox Code Playgroud)

如果在任何函数的开头调用此函数 - 它会将调用者中的最后一个arg视为"可变长度参数" - 支持任何约定.

例如,我可以像这样使用它

function sendEmail( body, recipients )
{
    recipients = lastArgumentAsParams();

    // foreach( recipient in recipients )...
}
Run Code Online (Sandbox Code Playgroud)

现在,我可以通过以下任何方式调用"sendEmail",它将按预期工作

sendEmail('hello world', "bill@microsoft.com" );
sendEmail('hello world', "bill@microsoft.com", "steve@apple.com" );
sendEmail('hello world', ["bill@microsoft.com", "steve@apple.com"] );
Run Code Online (Sandbox Code Playgroud)

JP *_*son 7

我个人更喜欢使用对象文字作为参数来支持命名参数,如下所示:

var myfunc = function(params){ //same as: function myfunc(params){....
  alert(params.firstName);
  alert(params.lastName);   
};

myfunc({firstName: 'JP', lastName: 'Richardson'});
Run Code Online (Sandbox Code Playgroud)

我认为它使代码非常易读,顺序无关紧要.

要么

您也可以访问该arguments对象.注意,它不是一个数组,但它是"类似数组".你可以在这里阅读:http://javascriptweblog.wordpress.com/2011/01/18/javascripts-arguments-object-and-beyond/

编辑:

你似乎在这里有误解.您正在使用短语"arguments object",并且认为它与对象文字符号相同.他们不是.

arguments对象允许您执行此操作:

function myfunc(){
  alert(arguments[0]); //JP
  alert(arguments[1]); //Richardson 
}

myfunc('JP', 'Richardson');
Run Code Online (Sandbox Code Playgroud)

这有帮助吗?