Why extend jQuery?

If you’ve been paying attention while reading through this app, you undoubtedly have noted that adopting jQuery for use in your pages has a profound effect on how a script is written within a page.

jQuery promotes a certain style for a page’s code: generally forming a jQuery collection and then applying a jQuery method, or chain of methods, to that collection. When writing your own code, you can write it however you please, but most experienced developers agree that having all of the code on a site, or at least the great majority of it, adhere to a consistent style is a good practice and one that we recommend. One good reason to pattern your code as jQuery extensions is to help maintain a consistent code style throughout the site. Another good reason is that by creating a reusable component, your future projects will benefit from it and other developers can also benefit if you publish the code on the web.

The final reason we’ll consider (though it’s possible others could list even more reasons) is that, by extending jQuery, you can use the existing code base that jQuery makes available to you. For example, by creating new jQuery methods, you automatically inherit the use of jQuery’s powerful selector mechanism and the cross-compatibility fixes to browser issues the library provides. Why write everything from scratch when you can layer on such powerful tools?

Given these reasons, it’s easy to see that writing your reusable components as jQuery extensions is a smart way of working. In the remainder of this lesson, we’ll examine the guidelines and patterns that allow you to create jQuery plugins and you’ll create a few of your own.

Before you start learning how to develop your own extensions, let’s see how you can find, judge, and use other developers’ plugins.


by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *