title: Javascript function argument handling
body: <p>It seems the best way to deal with arguments in javascript functions is to require the caller to pass an associative array:</p>
  
  <pre><code>example = function(options) {
    alert('option1: ' + options.a + ', option2: ' + options.b);
  }
  </code></pre>
  
  <p>Now arguments are named and not needed in any particular order when calling this function:</p>
  
  <pre><code>example({'b':'hello option2', 'a':'hello option1'});
  </code></pre>
  
  <p>The only thing I don't like is I have to have all this extra code to deal with required and default arguments, not to mention not allowing extra arguments so the caller knows they called the function wrong:</p>
  
  <pre><code>example = function(options) {
  var required_args = ['a', 'd'];
  var default_args = {'b':'option2', 'c':'option3'};
  var allowed_args = ['a', 'b', 'c', 'd'];
  // check required args
  // check allowed args
  for (var arg in default_args) {
      if (typeof options[arg] == "undefined")
                options[arg] = default_args[arg];
    }
    alert('option1: ' + options.a + ', option2: ' + options.b);
  }
  </code></pre>
  
  <p>Is there a standard way to deal with this?  I guess I can create a function like:</p>
  
  <pre><code>deal_with_args(options, required_args, default_args, allowed_args)
  </code></pre>
  
  <p>And throw some exception if required_args or allowed_args is violated...</p>