Re:Why jQuery.proxy rocks my world
by cfq on Wednesday April 27, 2011 @08:50AM
(#2489261)
This sort of thing happens a lot when people learn jQuery but not Javascript. This fascinating "jQuery feature" is just a wrapper for JavaScript's built-in functions call and/or it's brother apply.
Re:Why jQuery.proxy rocks my world
by udp on Wednesday April 27, 2011 @09:27AM
(#2489381)
Or rather, it returns a new function that wraps call/apply with a "baked in" value of 'this', so that it can be passed as a callback and retain the correct 'this'. That's a bit different than just being a wrapper for them - it's shorthand for using something like "var that = this" and a closure.
When we start seeing people using (jQuery.proxy(xxx)()) - then they've learned too much jQuery and not enough Javascript :-)
Re:Why jQuery.proxy rocks my world
by gmac on Wednesday April 27, 2011 @06:09AM
(#2488956)
Since the app is apparently written using backbone.js, the _.bindAll method would be a neater solution here (as I see one of the commenters on the post points out).
Re:Why jQuery.proxy rocks my world
by jashkenas on Wednesday April 27, 2011 @07:54AM
(#2489141)
Right, You may want to prefer using _.bind and _.bindAll to jQuery.proxy, because the Underscore functions delegate to the native Function.prototype.bind, if it exists.
Re:Why jQuery.proxy rocks my world
by mrspeaker on Wednesday April 27, 2011 @06:00AM
(#2488939)
I prefer to just use a closure - but that just might be because I've used them so long that my brain thinks it looks nicer.
Another nice context-setting trick is to set the context manually when you do ajax requests - then callbacks will have the context of the object you did the request in:
Re:Why jQuery.proxy rocks my world
by bialecki on Wednesday April 27, 2011 @09:23AM
(#2489364)
I don't know when jQuery added this method, but for the longest time they didn't have it and made you use closures even while most other javascript libraries had similar methods. Their argument was that you should know what's going on rather than just using a convenience method all over the place (and likely in situations where you don't need it).
Re:Why jQuery.proxy rocks my world
by dguaraglia on Wednesday April 27, 2011 @12:09PM
(#2489954)
I guess this is kind of surprising/interesting if you haven't taken a functional programming course in university, but as others have pointed out, proxy is just a simpler wrapper on top of 'apply'/'call'. You should read the docs on those two functions, as the difference in the way they work is subtle but worth understanding.
Re:Why jQuery.proxy rocks my world
by jcampbell1 on Wednesday April 27, 2011 @10:57AM
(#2489691)
The difference is that MooTools and Prototype modify the built in Function. Because of this, old versions of MooTools, and Prototype don't play nice with current browsers. This a a big reason jQuery won the war. function() {}.bind() is more convenient and cleaner, but $.proxy() will work regardless of whether Microsoft incorrectly implements a built in bind in their latest browser.
Re:Why jQuery.proxy rocks my world
by trezor on Wednesday April 27, 2011 @06:08AM
(#2488950)
Nice technique but it definitely illustrates the problems which Javascript's ill-defined this-keyword brings about (which was discussed here a few days ago).
This same problem could easily have been solved by setting a private variable (self, _this, cx, whatever) to this upon instantiation and referring to that instead of this.
In fact, I find myself using that technique (using self-defined variables instead of context) , especially for namespacing.
It makes your code easier to refactor with fewer hard-references to maintain. I'm about to completely give up any usage of "this" for a "var self = this;" upon object-creation.
Javascript context-handling is really just too messed up to be dependent on, and while it allows for lots of "cute" hacks, I personally find it creating a lot more problems than it solves.
https://developer.mozilla.org/en/JavaScript/Reference/Global...
https://developer.mozilla.org/en/JavaScript/Reference/Global...
When we start seeing people using (jQuery.proxy(xxx)()) - then they've learned too much jQuery and not enough Javascript :-)
https://developer.mozilla.org/en/JavaScript/Reference/Global...
Unfortunately, bind is a 5th edition feature, not widely available.
Another nice context-setting trick is to set the context manually when you do ajax requests - then callbacks will have the context of the object you did the request in:
It would look something like this
I'll check out backbone if I'm doing some jQuery dev.Oh how I wish mootools had won the JS library war...
This same problem could easily have been solved by setting a private variable (self, _this, cx, whatever) to this upon instantiation and referring to that instead of this.
In fact, I find myself using that technique (using self-defined variables instead of context) , especially for namespacing.
It makes your code easier to refactor with fewer hard-references to maintain. I'm about to completely give up any usage of "this" for a "var self = this;" upon object-creation.Javascript context-handling is really just too messed up to be dependent on, and while it allows for lots of "cute" hacks, I personally find it creating a lot more problems than it solves.