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
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).
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()
andfunc.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 tothis
, in the same way thatfunc.bind()
does, rather than implementing a mediator-system to attach and detach context from functions at-will.