views:

1567

answers:

13

Joel always said to be careful when using 3rd party libraries. From my initial impressions, JQuery is great. What should I beware of when using it? What are it's limitations? What headaches will I run into later on as I use it more?

+5  A: 

One thing I've run into with jQuery is that you end up chaining a lot of items together, and it tends to quickly get unreadable if you are not careful.

an example I can think of that illustrates this is on John David Anderson's blog:

There’s a guy who wrote a logging function so you can figure out whereYou(are).whenYoureCoding().inThe(middleOf).a(jQuery).trainWreck().

I can see the power of chaining things together, but my guess is you’ve probably gone too far if you’re needing to log things to the console mid-swing. There’s probably little to no chance you’re going to be able to read it a week from now, too.

mjc
I agree with the potential mess jQuery can do with some crazy selector plus daisy-chained methods.
Slace
In my own experience this is the only real drawback of JQuery. Chaining is one of its most powerful features, but it can be tricky to keep it readable. JQuery is small, fast, and amazing. (JOel's main product FogBugz even uses it.)
Stefan Rusek
This shouldn't be considered a downside of jQuery. You can write ugly, unmaintainable code in any language or framework. No need to knock jQuery for a power-feature that people abuse.
Ryan McGeary
So.. don't chain that much. I don't think I've ever chained more than three jQuery methods together for precisely this reason.
Eevee
Like any other feature of a programming language, it should be the developers responsibility to use it in a way that keeps it nice and readable. We cannot blame the chaining itself for that.
Marcel Tjandraatmadja
Chaining is fine, one of my co-workers was complaining about this until I put a new-line before each . in his chain and he found out it is just his formatting which was preventing him from being able to read his own code.
Greg
Almost every method in jQuery returns the jQuery object. Which means that you don't NEED to use chaining. E.g.: $("x").hide(); is the same as doing var x = $("x"); x.hide();
roosteronacid
STOP complaining, the JavaScript language lends itself to chaining!
J-P
+7  A: 

I've used it extensively and I have to admit, I'm yet to run into any serious brick walls! I have come up against a couple of bugs which I had to find a quick fix for myself, and then do extra testing with the next jQuery release to ensure that the bug had been dealt with properly, but that's something which applies to any 3rd party library rather than just jQuery.

I think it's a fantastic library I must say, and whilst the advice concerning 3rd party libraries has merit, with the amount of Javascript usage having rocketed in this Web 2.0 world, and with so many little discrepancies between browsers, having a well-maintained library can really speed up development as it saves you the overhead of having to do all the legwork yourself.

I guess if I was to issue one warning, it would be to make sure you don't go overboard with it - whilst it really accelerates Javascript development by abstracting away loads of logic you don't need to worry about, there's always the risk you'll start writing an inefficient application because you don't realise exactly what demands you're placing on the browser. I would therefore advise you do plenty of profiling with the likes of Firebug to check what's going on under the hood.

Luke Bennett
A: 

You can always consider different frameworks if jQuery doesn't suit. Here's an example of mootools.

MooTools is a compact, modular, Object-Oriented JavaScript framework designed for the intermediate to advanced JavaScript developer. It allows you to write powerful, flexible, and cross-browser code with its elegant, well documented, and coherent API.

michal kralik
A: 

I'm a big fan of jQuery (as evidenced by my having written both a plug-in and a Dashboard widget for it).

One thing to be aware of is which browsers jQuery supports. The docs site appears to be having issues at the moment. That's another thing to be aware of... ;-)

Andrew Hedges
+2  A: 

jQuery is great - it can do whatever javascript can do, but quicker, and in less code. Its only limitations are the ones inherent in javascript as a client-side scripting language. Like any tool, it's possible to missuse, but unless your scripting needs are profoundly basic there's almost no reason NOT to use it.

matt lohkamp
+2  A: 

I came across the following in my blog reading. It's not really limitations in jQuery but common mistakes made when using ASP.NET developers using jQuery:

http://encosia.com/2008/06/05/3-mistakes-to-avoid-when-using-jquery-with-aspnet-ajax/

Slace
+1  A: 

I've found jQuery to be indispensable when writing just about any useful bit of javascript. That said, one site I was working on wanted to do animations. I suggested NOT using flash, but performing the relatively "simple" animations that jQuery packages so well with jQuery. We used fades and slides and the like. In the end, it was too much for the browsers to handle (specifically IE, but FF showed signs of stress), and we had to scale almost all of it back.

jQuery is tons of fun to code with, and experiment with. It has a fantastic developer community that fields questions very quickly. Just be careful not to get too carried away! :)

shmuel613
A: 

My question always is about jQuery, do you really need it? You can easily build a quick ajax class on your own if that is what you want from jQuery, but do you need everything else? I always prefer to learn from other programs/libraries and then build my own so it fits my purposes perfectly rather than use a bloated library that I have to try really hard to make it work. Plus, what do you really learn by just using someone elses code? If you have any issues that appear, it is much harder to fix them if you do not know the code

BrilliantWinter
You sound like a fool!
J-P
+6  A: 

@ mjc

$("a tip")
.you()
.can()
.chain()
.stuff()
.like()
.this();

And/or define a variable, for which to use the jQuery functions on:

var $tip = $("a tip");
$tip.choo();
$tip.choo();
$tip.train();
roosteronacid
+2  A: 

@ BrilliantWinter

jQuery is not at all bloated. It's one of the smallest libraries out there.

All it's functions are extends of the jQuery object, which means you can detach whatever functionality you don't use, and make the footprint of the library even smaller than it's default size (15kb, Minified and Gzipped).

jQuery - and every other library for that matter - provides an API which is the same across all A-grade browsers. This abstraction leaves your code cleaner and less error-prone.

Finally, jQuery is used by major "players". Companies like Google, Dell, Digg and NBC use the library. This is not only a big seal-of-approval, but also an assurance that the developers of jQuery are very careful when revising the library, making sure nothing breaks and no bugs are introduced.

roosteronacid
A: 

@roosteronacid

You learn something new every day ;) I had not really used jQuery before, so I was just assuming it was similar to the other javascript libraries out there. I have been thinking about it and am actually considering implementing it in my own website. Thanks for the info!

BrilliantWinter
+2  A: 

@BrilliantWater - Most people don't use jQuery to "learn", they use it because it's quicker and easier to use and causes less headaches than creating all the methods yourself. And the whole "bloated" argument is totally moot; jQuery is one of the smallest libraries out there and with more and more people getting broadband it's becoming less and less of an issue.

Plus, since jQuery is hosted by GoogleCode it's likely that it'll already be in the users cache since so many websites use it!

jQuery is awesome! I keep saying to myself that I need to learn another library but I really don't. jQuery has everything I need. I know it's not suited to all projects but it certainly has a place in most!

J-P
A: 

I've been using it for about 6 months now and except for some of the slower developers on my team not embracing it (probably because of the intimidating appearance of some of the longer chains) I haven't run into a single problem.

ctrlShiftBryan