title: jQuery.proxy vs. underscore.bind
body: <p>In the context of using methods as event handlers (i.e. <code>$(...).on('something', myObject.handleSomething)</code>). I discovered the relatively large performance difference between $.proxy and _.bind (<a href="http://jsperf.com/bind-vs-jquery-proxy/27" rel="noreferrer">http://jsperf.com/bind-vs-jquery-proxy/27</a>) and looked at their implementation.</p>
  
  <p>jQuery (<a href="http://james.padolsey.com/jquery/#v=1.10.2&amp;fn=proxy" rel="noreferrer">http://james.padolsey.com/jquery/#v=1.10.2&amp;fn=proxy</a>) ends up returning:</p>
  
  <pre><code>args = core_slice.call(arguments, 2);
  proxy = function () {
      return fn.apply(context || this, args.concat(core_slice.call(arguments)));
  };
  </code></pre>
  
  <p>while underscore (<a href="http://underscorejs.org/docs/underscore.html#section-60" rel="noreferrer">http://underscorejs.org/docs/underscore.html#section-60</a>) ends up returning (ctor is <code>var ctor = function(){};</code>):</p>
  
  <pre><code>args = slice.call(arguments, 2);
  return bound = function() {
    if (!(this instanceof bound)) return func.apply(context, args.concat(slice.call(arguments)));
    ctor.prototype = func.prototype;
    var self = new ctor;
    ctor.prototype = null;
    var result = func.apply(self, args.concat(slice.call(arguments)));
    if (Object(result) === result) return result;
    return self;
  };
  </code></pre>
  
  <p>I understand that <code>_.bind</code> will allow me to bind arguments for a <code>new</code> call, but will it have any practical advantage if I only want to use <code>myObject.handleSomething</code> as an event handler?</p>
  
  <p>Is it possible to write something similar to <code>_.bindAll</code> using <code>$.proxy</code>? E.g.</p>
  
  <pre><code>$.proxyAll = function (obj) {
      for (var attr in obj) if (obj.hasOwnProperty(attr) &amp;&amp; $.isFunction(obj[attr])) {
          obj[attr] = $.proxy(obj[attr], obj);
      }
      return obj;
  };
  </code></pre>