开发者

What are the advantages of jQuery over writing JavaScript myself? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

When should I use a javascript framework library?

All,

I'm fairly new to web app development.

Recently, I've built a few HTML5 apps specifically targeted at iPads and iPhones.

I've noticed that very few people seem to use straight JavaScript - virtually everyone uses jQuery (Prototype, YUI, Mootools, etc. seems somewhat less popular).

If I'm starting out - SHOULD I use jQuery? Or, would I be better off writing my own JavaScript?

In general - I prefer using as much of "my own" code as possible. I'm uncomfortable using a framework if I can avoid it - I feel like it prevents me from understanding what's going on "underneath the hood."

(Sure - even if I'm using straight JavaScript - there are many levels "underneath the hood" (e.g., browser's JavaScript enging, the OS, kernel, etc.), that I don't understand, but the lower the level I do understand, the better.)

The only concrete advantage I can think of for not using jQuery... loading the ENTIRE jQuery framework, when I only need a handful of it's functions - seems unnecessary and wasteful.

So - in general - would I be better off creating my own, highly-tailored, application-specific JavaScript for each project?

Or - should I bite the bullet, get over my reluctance to use someone else's code, and use jQuery? Are there advantages to it besides just not having to create my own versions of functions that have already been written by someone else?

E.g., are it's functions somehow optimized or more efficient than what I could produce on my own?

[UPDATE]

One of the common themes in the answers so far... jQuery eliminates the need to worry about browser differences. Good point.

By in my case, I'm writing apps specific开发者_如何学编程ally for the iPhone, iPad, and Android. As far as I know - the browsers are pretty consistent. The vagaries and idiosyncrasies of IE6, 7, and 8 really don't seem to apply. Is that true? If so, does it eliminate a major reason for using jQuery?

Another key issue... jQuery is so popular, that there's a good chance that it's already in the user's cache, thus eliminating the need to load it. True... but is that the only performance hit? In other words... does the existence of the hundreds of functions that are part of jQuery impact the performance of my page? (I don't know how JavaScript engines work, so I have no idea if this is the case...).

Many thanks in advance for your advice and guidance.


I do understand you. I had the same thoughts for quite a while. However, in this particular instance it has a good reason why so many people are using a framework (jQuery as the most popular nowadays). The reason is, not to care about browser-differences + a nice toolkit of methods and functions.

But the first thing (browser differences) is by far more important. If you're going to write everything on your own, you just have to care about SO SO many things for all those browsers and versions out there, it's just disgusting. So some smart clever people came up with the idea, why not creating a library which abstracts all those differences and we can use the same set of methods on almost any browser platform. Boohja, that is how a Javascript library is born.

Another point you mentiond: loading the ENTIRE jQuery framework (...)

Well, as you also correctly mentioned, jQuery is so popular it's so likely that an user already has a jQuery-version cached in his browser. So it's always a good idea to first try to load a library like jQuery from a common CDN like Google. The chances that the browser don't even need to download it are pretty good.

Now I'm not gonna argue about the code quality and stuff like that. But jQuery is a pretty solid lib. It offers a very nice browser abstraction along with a convinient syntax (jAndys opinion). But there are lots of other very nice librarys out there. In general I would always recommend to use one, especially for large scaled web applications.

At some point, you'll start anyway to write your own methods to abstract browser differences and behaviors on your own, because it is just annoying to write so much more code every time. Therefore no need to invent the wheel here (only for learning purposes probably).


This is more a question of philosophy than anything else. jQuery is highly optimized for performance, lightweight and there's a lot of things that are just tedious to do by hand every time. If you have a project, that only needs 4-5 functions, there's probably no need to use a library.

But if there's an application that uses class/id/attribute selectors, animations, listens to events, etc., then you should "bite the bullet".

There's no reason not to go with both approaches imo, just choose according to the project.


My opinion is that if you think that you can bend javascript to your will and not need a framework, then go for it.


I'd go for jQuery if I were you, simply because of the cross-browser quirks JavaScript has. jQuery handles this for you.

E.g., are it's functions somehow optimized or more efficient than what I could produce on my own?

Probably, although there might be a few exceptions.


JavaScript (web browsers) should be standardized, but they not (yet). jQuery do that!


You might be interested in list of tiny JS frameworks, if you think jQuery is too large.


Yes, jQuery hides a lot from you. Yes, a page with jQuery will be slower than an optimized page without jQuery. But computers are so powerful these days that, unless you are using jQuery in a very inefficient way in huge applications, nobody will notice. About the download issue: it is only 31KiB. Even with GPRS this will only take a couple of seconds to load.

My advice: learn jQuery, and then use it when appropriate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜