What is the difference between $.proxy() and bind()?

Max Cantor picture Max Cantor · Sep 19, 2012 · Viewed 14.6k times · Source

In 2009, ECMAScript 5 added a built-in bind() function which takes an object as a parameter and returns an identical function in which this will always refer to the object you passed it. (I couldn't find anything that looked like a canonical documentation link.)

How is this different from jQuery's $.proxy() function? Did $.proxy() come first before ECMAScript 5 was released? Is there a particular reason to favor $.proxy(function(){}, this) over function(){}.bind(this)?

Answer

Matt Whipple picture Matt Whipple · Sep 19, 2012

proxy came first and you should likely favor bind as it is a standard. The way they are called varies slightly (due to being attached to Function.prototype vs just being a function) but their behavior is the same.

There is a pretty good post here: jQuery.proxy() usage, that ends with that advice.