views:

820

answers:

8

We're considering switching our site from Prototype to jQuery. Being all-too-familiar with Prototype, I'm well aware of the things about Prototype that I find limiting or annoying.

My question for jQuery users is: After working with jQuery for a while, what do you find frustrating? Are there things about jQuery that make you think about switching (back) to Prototype?

+2  A: 

(The only thing I can think of is that this is the element instead of a jQuery object in $("...").each(function)-calls, as $(element) is more often used then just the element. And that extremly minor thing is just about it.

Example of the above (simplified and I know that there are other much better ways to do this, I just couldn't think of a better example now):

// Make all divs that has foo=bar pink.
$("div").each(function(){
  if($(this).attr("foo") == "bar"){
    $(this).css("background", "pink");
  }
});

each is a function that takes a function as parameter, that function is called once for each matching element. In the function passed, this refers to the actual browser DOM-element, but I find that you often will want to use some jQuery function on each element, thus having to use $(this). If this had been set to what $(this) is, you'd get shorter code, and you could still access the DOM element object using this.get(0). Now I see the reason for things being as they are, namely that writing $(this) instead of this, is hardly that cumbersome, and in case you can do what you want to do with the DOM element the way it is is faster than the way it could have been, and the other way wouldn't be faster in the case you want $(this).)

svinto
You lost me there. I think I know what you mean, but, could you explain it a little better?
Juan
Juan: I added a huge example and explanation-in-detail-block, hopefully crystal clear.
svinto
+6  A: 

I think the only that gets me is that when I do a selection query for a single element I have to remember that it returns an array of elements even though I know there is only one. Normally, this doesn't make any difference unless you want to interact with the element directly instead of through jQuery methods.

tvanfosson
That's about it for me, as well. It's minor, though.
EndangeredMassa
+1  A: 

Nope. Nada. Nyet.

sanchothefat
+2  A: 

My two pain points have been the bracket hell, can get very confusing

$('.myDiv').append($('<ul />').append($('<li />').text('content')));

My other common issue has to do with the use of JSON in jQuery, I always miss the last comma,

$('.myDiv').tabs({ option1:true, options2:false(, woops)});

Finally, I've been using jQuery for about 6 months now and I don't think I'll ever go back to prototypes. I absolutely love jQuery, and a lot of the tricks they use have helped me learn a lot. one cool trick that I like is using string literals for method calls, I never really did that too much with prototypes.

$('.myDiv')[(add ? 'add' : 'remove') + 'Class']('redText');
bendewey
You should really line feed and indent your code. JQuery syntax gets pretty messy if you're trying to one-line everything.
Soviut
This isn't jquery specific. You can do this with any javascript method in any object, because objects are just key/value pairs, irrespective of their type.
steve_c
@steve_c I realize that this is standard javascript syntax stuff. It's just a pain point that I've found from using jQuery which relies on this more than prototyping. In addition I also realize that my 'cool trick' isn't jQuery specific, but I noted that.
bendewey
Why would you use all those brackets when $(".myDiv").append("<ul><li>content</li></ul>"); achieves the same?
sanchothefat
@sanchothefat admittedly that might not have been the best example, I'm not sure if your sample has the same performance, but the point still stands that there are cases in jquery where the brackets and parens can be confusing.
bendewey
+3  A: 

Probably the only real issue I've ever ran into is $(this) scope problems. For example, if you're doing a nested for loop over elements and sub elements using the built in JQuery .each() function, what does $(this) refer to? In that case it refers to the inner-most scope, as it should be, but its not always expected.

The simple solution is to just cache $(this) to a variable before drilling further into a chain:

$("li").each(function() {
    // cache this
    var list_item = $(this);
    // get all child a tags
    list_item.find("a").each(function() {
        // scope of this now relates to a tags
        $(this).hide("slow");
    });
});
Soviut
A: 

.each:

jQuery (you need Index, even if you're not using it):

$.each(collection, function(index, item) {
  item.hide();
});

Prototype (you're usually using the item, so you can omit the index):

collection.each(function(item) {
  item.hide();
});
Diodeus
I don't believe that's true. "this" is supposed to point to the element in question. You should be able to do $.each(collection, function() { $(this).hide(); }); You could also do collection.each(function() { $(this).hide(); });
EndangeredMassa
+1 .
svinto
+1 @ EndangeredMassa
Gareth
Yep this is completely misleading. No one would ever write jQuery code like in this comment... Or at least they shouldn't be :P
thenduks
A: 

This is really only an annoyance if you're doing a lot of DOM manipulation. PrototypeJs automatically adds its API to DOM Elements, so this works in prototypejs (jQuery of course doesn't do this):

var el = document.createElement("div");
el.addClassName("hello"); // addClassName is a prototypejs method implemented on the native HTMLElement

Even without running the native element through the $() function.

PS: Should note that this doesn't work in IE.

Luca Matteis
+2  A: 

I don't think there are any real gotchas, or even any lingering annoyances. The other answers here seem to confirm this - issues are caused simply by the slightly different API and different JavaScript coding style that jQuery encourages.

I started using Prototype a couple of years ago and found it a revelation. So powerful, so elegant. After a few months I tried out jQuery and discovered what power and elegance really are. I don't remember any annoyances. Now I am back working on a project using Prototype and it feels like a step back (to be fair, we're using Prototype 1.5.1).

If you reversed the question - "What Prototype annoyances should I be aware of as a jQuery user?" - you would get a lot more answers.

Bennett McElwee