views:

67

answers:

3

This isn't about a side-by-side technical comparison, rather about how to "think in jQuery" versus "thinking in Prototype".

I've used Prototype heavily for several years, and jQuery somewhat less heavily until about a year ago when I started doing a lot with it.

With Prototype, I can write some fairly elegant code; my boss once reviewed a large amount of my code and remarked that it was the first Javascript he'd ever found a pleasure to read. I understand - and understood pretty much from the beginning - almost instinctively what Prototype's trying to do, and know how to work with it.

My jQuery code is a lot more, how can I put this, "workmanlike". I feel as if I'm fighting jQuery every step of the way. I have to (try to) force myself to stick with it and not drop down into "native" JS, where I know I could bash out clean cross-browser code more quickly. Working with it more makes it more, not less, frustrating.

It's not (or at least not entirely) a lack of familiarity with the functions available. I'll often know I need to use a given function, but the way in which it's used seems truly bizarre. That's usually a sign that I'm coming at something entirely the wrong way.

The more I think about this, the more I think I'm trying to use jQuery in a Prototype way.

There has to be some blinding flash of light that hasn't happened to me yet. Especially if you've worked a lot with both, what do you find are the most fundamental differences in approach? How do you need to adjust your mindset when switching from one to the other?

Don't be afraid to state the blindingly obvious, because it may just be that blinding flash...

+4  A: 

I went through that transformation. The main thing to tell yourself over and over again is that jQuery is, first and foremost, about making DOM manipulation easier and more cross-platform safe. There's no "reduce" (Prototype used to call it "inject", I think) in jQuery. Why? Because the maintainers don't consider it important for the primary task of jQuery.

Thus, the way that Prototype's base object extensions creep into your coding style as you write your code to get your own work done, well, that pretty much doesn't happen in plain ol' jQuery. (See, however, the lovely Underscore.js library for a way to get some of that functionality in a jQuery-friendly way.)

For me, that's made it easier to figure out how to build on jQuery. It's just a different sort of thing. Now, jQuery is very solid and it really does make DOM manipulation and HTML wrangling a lot nicer than what you get from plain Javascript. (I think Prototype does an OK job too, but jQuery is super-focused on the problem.)

Pointy
+1  A: 

The best advice i can give is "Embrace this". In jQ, you're nearly always talking about iterating over a set that is wrapped in the jQuery object. Invoking one of the set's methods performs the method on all the elements of the set, whether its 1 or 100. That method is always going to return the same instance of the set (aside from accessors that get a property). In the context of the interation this is the value of the item in that set youre manipulating - usually the raw DOM Element, but it could be the value of an object property or array item.

prodigitalson
"In jQ, you're nearly always talking about iterating over a set that is wrapped in the jQuery object." This helps, definitely, and may just make this less of a problem:"Invoking one of the set's methods performs the method on all the elements of the set, whether its 1 or 100." This (heh) certainly makes it harder to understand others' code. There doesn't seem to be any obvious "HEY! This is iterating over a set" construct, like Prototype's each(), and that drives me nuts.
Ed Daniel
Well there is `$.each` for iterating over non-jQuery traversable objects (arrays, anon objects, other constructs) and ther is `$('selector').each(functionToPerform)` for applying non jQuery functions to the set. But all you have to do is look at the selector to know what the contents of the set are.
prodigitalson
A: 

Why do you need to think differently? Instead of adapting your style to every framework or language that comes along, why not adapt the framework or language itself to your liking. Then all you'll have to do is be open to the idea that there might be better ways of writing or structuring code than you already know, and when those ways present themselves, objectively analyze and then include them in your repository.

The choice is almost never all or nothing. Both frameworks have great offerings, and you can use techniques from both in harmony for building a great application.

Anurag