title: Javascript convention for variable length arguments
body: <p>I am getting more in to javascript development, and want to ensure I am following popular conventions.
  Currently I have a library which consists of functions that can be passed either 1 model to operate on, or many models.</p>
  
  <p>Given the climate that a few javascript libraries are very popular, I am curious; would I be conforming to the 'defacto standard' by achieving my 'single-item or list-of' requirement, by enumerating the arguments variable, or by allowing one of the arguments to be an array?</p>
  
  <p><strong>Scenario 1: argument enumeration</strong></p>
  
  <pre><code>// passing a single entity to my function
  sendMail( email, recipient1 );
  
  // passing multiple entities to my function
  sendMail( email, recipient1, recipient2 );
  </code></pre>
  
  <p><strong>Scenario 2: entity argument is either single instance, or array</strong></p>
  
  <pre><code>// pass a single entity
  sendMail( email, recipient1 );
  
  // passing multiple entities
  sendMail( email, [recipient1, recipient2] );
  </code></pre>
  
  <p>I have seen areas of jQuery which use 'scenario 2', but I would still like to ask - which approach is the most popular, and why?</p>
  
  <p>Thanks</p>
  
  <p>[EDIT]</p>
  
  <p>A couple of comments have followed the same vein, of using an arguments object - which is similar to 'scenario 2' - but I feel it introduces unnecessary complexity - the elements dont need to be named, because they are just a variable length list. I thought I would just add that here in case my question wasn't clear enough.</p>
  
  <p>[EDIT]</p>
  
  <p>I see code like this all through jQuery-1-7.js</p>
  
  <pre><code>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 || [];
      }
  }
  </code></pre>
  
  <p>[EDIT]</p>
  
  <p>After some discussion with JP, I came up with this - which I'm not saying is the right choice, but it is very flexible...</p>
  
  <pre><code>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 &amp;&amp; jQuery.isArray(callerArgs[0]))
          return callerArgs[0];
      else
          return callerArgs;
  }
  </code></pre>
  
  <p>If you call this function at the beginning of any function - it will treat the last arg in the caller as a 'variable length argument' - supporting any of the conventions.</p>
  
  <p>For example, I can use it like this</p>
  
  <pre><code>function sendEmail( body, recipients )
  {
      recipients = lastArgumentAsParams();
  
      // foreach( recipient in recipients )...
  }
  </code></pre>
  
  <p>Now, I can call 'sendEmail' in any of the following ways and it will work as expected</p>
  
  <pre><code>sendEmail('hello world', "bill@microsoft.com" );
  sendEmail('hello world', "bill@microsoft.com", "steve@apple.com" );
  sendEmail('hello world', ["bill@microsoft.com", "steve@apple.com"] );
  </code></pre>