Summary (^) ❘ 265
$(document).unbind('mousedown.contextMenu');
}
In this block of code, events are removed and data is set to false, reversing all the document changes
that you put in place to enable your context menu. Because your events are namespaced using the
contextMenu name, only the events that you explicitly attached are removed. If other people attached
click or mouseover or other events using other names in other scripts, their events remain untouched
and functional.
Good Practice for jQuery Plugin Development
There are just a few things you should keep in mind while developing your own jQuery plugins:
➤ (^) It’s considered good practice to always expect one or more items to be passed to your plugin
(in the jQuery selection preceding the call to your plugin), and to always return the jQuery
object, whenever it makes sense and is possible to do so, which makes it possible to chain
multiple method calls together.
➤ (^) If you plan on using third-party jQuery plugins, you’ll want to consider name-spacing your
own plugins in some way so that your naming choices don’t confl ict with those of third-party
plugins. Oftentimes this is done by prefi xing a name of some sort to your plugin name, like
the name of your company or organization. In the context of your context menu plugin, if
that plugin were developed by your company Example, you might end up calling your plugin
exampleContextMenu().
➤ (^) Avoid polluting the global namespace. Place all your function calls and variables inside your
jQuery plugin. As an added benefi t, this can also make your APIs private and callable only by
you. Preventing other people from using your private APIs gives you the freedom to change
them when you need to because you don’t have to worry about supporting the people using
those APIs.
➤ (^) If you’re interested in developing offi cial third-party jQuery plugins that follow all the recom-
mended best practices published by jQuery’s developers, see the document located at http://
docs.jquery.com/Plugins/Authoring.
Summary
In this chapter, you learned the basic concepts needed to author your own jQuery plugin. You learned
how jQuery plugins are created by passing an object literal to jQuery’s $.fn.extend() method.You learned how jQuery plugins expect to have one or more items passed to them, which are always
present in the this keyword.When you write jQuery plugins, you should return the jQuery object (when it makes sense and is
possible to do so) because this preserves jQuery’s capability to chain method calls onto one another.You learned how to write a more complicated, more realistic jQuery plugin example by creating a
context menu plugin.