Why jQuery works well
With the resurgence of interest in dynamic HTML comes a proliferation of JavaScript frameworks. Some are specialized, focusing on just one or two of the above tasks. Others attempt to catalog every possible behavior and animation, and serve these all up pre-packaged. To maintain the wide range of features outlined above while remaining relatively compact, jQuery employs several strategies:
Leverage knowledge of CSS: By basing the mechanism for locating page elements on CSS selectors , jQuery inherits a terse, yet legible, way of expressing a document's structure. The jQuery library becomes an entry point for designers who want to add behaviors to their pages because a prerequisite for doing professional web development is to have knowledge of CSS syntax.
Support extensions: In order to avoid feature creep, jQuery relegates special-case uses to plugins . The method for creating new plugins is simple and well-documented, which has spurred the development of a wide variety of inventive and useful modules. Most of the features in the basic jQuery download are internally realized through the plugin architecture, and can be removed if desired, yielding an even smaller library.
Abstract away browser quirks: An unfortunate reality of web development is that each browser has its own set of deviations from published standards. A significant portion of any web application can be relegated to handle features differently on each platform. While the ever-evolving browser landscape makes a perfectly browser-neutral code base impossible for some advanced features, jQuery adds an abstraction layer that normalizes the common tasks, reducing the size of code while tremendously simplifying it.
Always work with sets: When we instruct jQuery, "Find all elements with the class
collapsible
and hide them," there is no need to loop through each returned element. Instead, methods such as.hide()
are designed to automatically work on sets of objects instead of individual ones. This technique, called implicit iteration , means that many looping constructs become unnecessary, shortening code considerably.Allow multiple actions in one line: To avoid overuse of temporary variables or wasteful repetition, jQuery employs a programming pattern called chaining for the majority of its methods. This means that the result of most operations on an object is the object itself, ready for the next action to be applied to it.
These strategies have kept the jQuery package slim—roughly 30 KB, compressed—while at the same time providing techniques to keep our custom code that uses the library compact.
The elegance of the library comes about partly by design, and partly due to the evolutionary process spurred by the vibrant community that has sprung up around the project. Users of jQuery gather to discuss not only the development of plugins, but also enhancements to the core library. The users and developers also assist in continually improving the official project documentation, which can be found at http://api.jquery.com.
Despite all of the efforts required to engineer such a flexible and robust system, the end product is free for all to use. This open source project is dually licensed under the MIT License (to permit free use of jQuery on any site and facilitate its use within proprietary software) and the GNU Public License (appropriate for inclusion in other GNU-licensed open-source projects).