title: Setting length on wrapper functions
body: <p>I often find myself needing to wrap a function on an object for various purposes. Is there an elegant way to preserve the <code>length</code> property of the original function on the wrapper function?</p>
  
  <p>For example:</p>
  
  <pre><code>var x = {
      a: function(arg1, arg2) { /* do something */ }
  };
  
  function wrap(obj) {
      var original = obj.a;
      obj.a = function wrapper() {
          console.log('a called');
          original.apply(this, arguments);
      };
  }
  
  x.a.length; // =&gt; 2
  wrap(x);
  x.a.length; // =&gt; 0
  </code></pre>
  
  <p>What I would like to do:</p>
  
  <pre><code>var x = {
      a: function(arg1, arg2) { /* do something */ }
  };
  
  function wrap(obj) {
      var original = obj.a;
      obj.a = function wrapper() {
          console.log('a called');
          original.apply(this, arguments);
      };
      obj.a.length = original.length;
  }
  
  x.a.length; // =&gt; 2
  wrap(x);
  x.a.length; // =&gt; 2 (actual still 0)
  </code></pre>
  
  <p>However, this doesn't work because <code>length</code> isn't writable.</p>
  
  <p>The only solutions I can come up with at the moment are to (1) dynamically generate the function as a string and <code>eval</code>/<code>new Function</code> it or (2) have a giant array of proxy functions of different lengths and select the proxy function corresponding to the correct length. This doesn't seem like an elegant solution to me, and it seems a reasonable request to be able to create a function with arbitrary <code>length</code> <em>without</em> specifying each argument in the function.</p>
  
  <p>It seems like <code>bind</code> is able to do this internally:</p>
  
  <pre><code>function a(b, c) { }
  a.bind(null).length; // =&gt; 2
  </code></pre>
  
  <p>This creates a wrapper function with the length of the original! That's exactly what I want to be able to do myself.</p>
  
  <p>Is there any other way?</p>