Re:Why I still prefer Prototype to jQuery
by fh on Monday January 12, 2009 @05:37PM
(#431257)
Not having extensively used either jQuery or Prototype, I found this comparison interesting. But the entire discussion about jQuery "stealing" `this` is somewhat misinformed. Javascript closures don't caputure the value of `this`; instead, the value of `this` is determined at the call site. If the function is called as a method (using the `object.method()` syntax), `this` is bound to `object`; otherwise, `this` is bound to the global object (called `window` in browsers). The behavior I just described makes `this` is almost useless in Javascript closures, and the "idiomatic Prototype" example the author gives cannot possibly work:
Re:Why I still prefer Prototype to jQuery
by litewulf on Monday January 12, 2009 @06:09PM
(#431294)
I will say this, use of this is very scary and requires me to look at the docs more often (need to know where in the scope chain this is being set).
In fact, I'd argue that in general seeing uses of this outside of object oriented code make me sit up and look very carefully, because its really easy to get spooky "action at a distance" type behaviour.
In general I prefer the technique you described as it is the most frequent use case and simplifies the implementation of the function that executes the callback.
Re:Why I still prefer Prototype to jQuery
by fh on Tuesday January 13, 2009 @07:50AM
(#431968)
Note how you haven't used the keyword `this` inside the closure. What you've written is functionally equivalent to the technique I described, assigning the `this` object to a temporary variable. The only difference is that you bind it with a lambda instead of a `var`.
Re:Why I still prefer Prototype to jQuery
by old-gregg on Monday January 12, 2009 @05:50PM
(#431278)
I don't understand why does one need to make a choice. Even comparing the two makes little sense: jQuery is smaller and DOM-focused, while Prototype is a much more comprehensive multi-file library and DOM-related stuff is just a part of it, completely isolated in its own file which you can exclude during Prototype's build process.
I use both: jQuery for HTTP and DOM traversing/manipulation plus DOM-less prototype core for everything else. This combo is awesome, conflict-free and quite compact when gzipped. Bare bone JavaScript is too weak library wise and Prototype makes an excellent "standard library" for it. I couldn't live without Prototype's extensions to enumerable, strings, arrays, hashes, etc.
And it's 100% compatible with Rails thanks to jRails project. I highly recommend it.
Re:Why I still prefer Prototype to jQuery
by litewulf on Monday January 12, 2009 @06:17PM
(#431306)
One argument against doing both is that Prototype alters many built in classes directly. This is often risky and dangerous, and makes interactions between the two code bases possible.
Re:Why I still prefer Prototype to jQuery
by jpcx01 on Monday January 12, 2009 @07:42PM
(#431417)
Prototype alters stuff but jQuery doesn't. So there is very little conflict. The use of Prototype however will prevent use of other altering js frameworks like MooTools.
Fundamentally I think Prototype/MooTools are horribly designed javascript library because they introduce incompatibilities. However I use them when I need a free component or 2. I would never drop jQuery though so I always use $j instead of $, even when using jquery by itself
Re:Why I still prefer Prototype to jQuery
by old-gregg on Monday January 12, 2009 @09:16PM
(#431509)
A correction: it's better to say "Prototype expands built-in classes", i.e. it adds methods that otherwise aren't there. This is never risky nor dangerous unless you have another global-namespace library, which jQuery isn't.
Re:Why I still prefer Prototype to jQuery
by jkkramer on Monday January 12, 2009 @05:12PM
(#431236)
It's nice to see a level-headed assessment like this. Too many programmers rely on trends or religious convictions to make decisions. All choices involve trade-offs, so make yourself aware of them!
Re:Why I still prefer Prototype to jQuery
by jgfoot on Monday January 12, 2009 @08:24PM
(#431461)
JavaScript 1.6 added seven new array methods (indexOf, lastIndexOf, every, filter, forEach, map, and some). If only backward compatibility weren't a problem, the warring JavaScript libraries could just use these (or implement them into the prototype like Sugar Arrays, http://www.dustindiaz.com/sugar-arrays/) instead of creating their own quirky methods.
Re:Why I still prefer Prototype to jQuery
by larrywright on Monday January 12, 2009 @10:53PM
(#431605)
I don't have a huge amount of experience with either framework, but I've used them both. One thing that stands out about jQuery is that it's unobtrusive - Prototype is not (at least not without some help from something like Lowpro).
Re:Why I still prefer Prototype to jQuery
by deadwisdom on Tuesday January 13, 2009 @01:55PM
(#432453)
jQuery is light, but it has a trove of plugins that will help you with more complex tasks. The core is purposefully limited in scope, not trying to do everything for you; it only provides what Javascript already should, but doesn't.
Re:Why I still prefer Prototype to jQuery
by GrandMasterBirt on Monday January 12, 2009 @07:54PM
(#431432)
Argument order:
jQuery
$.each(collection,function(){
this.bla();
});
The reason index is first param is because wheb you need it it is the only param. The reason for the item param is incase you need to attach a scope object to be this!
In fact, I'd argue that in general seeing uses of this outside of object oriented code make me sit up and look very carefully, because its really easy to get spooky "action at a distance" type behaviour.
$A(self.columns).each(function(item) { this.buildHeader(item); }, this);
In general I prefer the technique you described as it is the most frequent use case and simplifies the implementation of the function that executes the callback.
Hmm??
e.onmouseover = function(c) {return function() { c.dosomething()}}(this);
Then in the dosomething function, "this" references what you expect...
I don't see why you need a library to construct a simple closure.
I use both: jQuery for HTTP and DOM traversing/manipulation plus DOM-less prototype core for everything else. This combo is awesome, conflict-free and quite compact when gzipped. Bare bone JavaScript is too weak library wise and Prototype makes an excellent "standard library" for it. I couldn't live without Prototype's extensions to enumerable, strings, arrays, hashes, etc.
And it's 100% compatible with Rails thanks to jRails project. I highly recommend it.
Fundamentally I think Prototype/MooTools are horribly designed javascript library because they introduce incompatibilities. However I use them when I need a free component or 2. I would never drop jQuery though so I always use $j instead of $, even when using jquery by itself
It's a very C-like way of doing things:
but i wouldn't call this article level-headed, it has an explicitly stated tilt towards prototype.
Some nice speed improvements are coming to jQuery 1.3:
http://www.flickr.com/photos/jeresig/sets/72157612498756067/
the last time i used prototype was in 2007, now it's all jquery
jQuery
$.each(collection,function(){ this.bla(); });
The reason index is first param is because wheb you need it it is the only param. The reason for the item param is incase you need to attach a scope object to be this!