Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)?

like image 578
Max Cantor Avatar asked Sep 19 '12 19:09

Max Cantor


People also ask

What is bind () in JS?

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

What is the purpose of a bind?

The bind() method allows an object to borrow a method from another object without making a copy of that method. This is known as function borrowing in JavaScript.

What is proxy in jquery?

Definition and Usage. The $. proxy method takes an existing function and returns a new one with a particular context. This method is often used for attaching events to an element where the context is pointing back to a different object. Tip: If you bind the function returned from $.

What is proxy in JavaScript?

Proxy is an object in javascript which wraps an object or a function and monitors it via something called target. Irrespective of the wrapped object or function existence. Proxy are similar to meta programming in other languages.


2 Answers

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.

like image 179
Matt Whipple Avatar answered Oct 05 '22 02:10

Matt Whipple


Edit

Please pay no attention to this post (despite being the accepted answer).
Long story short, it was my own fault for making assumptions about the context of the question, rather than just looking up the API docs, and was accepted as the answer before I could realize my own stupidity (making assumptions, without validating them) and delete it.

Matt Whipple's answer is 100% correct, and while I disagree with his statement that real Proxies are useless in JS (they would be fantastic in some low-level concerns), the rest of his statements are flat-out objectively correct (aside from actual dates for .bind vs .proxy, as .bind was in the spec years before it landed consistently in browsers).

Below is my shame, in the stocks for all to see...

Feel free to throw tomatoes at it.
If you want to know why I answered the way I did, read the comments below.


The difference between $({}).proxy() and func.bind({}) is that proxy is a loose connection. You can detach at any time.

That's sort of what proxies are for. The invisible-interface between what you want to do and the thing that will actually do it.

For the record, there's also a $.bind() which is not a proxy. That is to say, it fully binds to this, in the same way that func.bind() does, rather than implementing a mediator-system to attach and detach context from functions at-will.

like image 36
Norguard Avatar answered Oct 05 '22 04:10

Norguard