jQuery
New Wave Javascript


jQuery offers a fully-extensible framework for adding in new methods and functionality. Most of the plugins included in the default download are written using the jQuery plugin construct.

Plug-in writing comes in two steps. The first is writing any of your public methods, for example:

$.fn.debug = function() {
  return this.each(function(){
    alert(this);
  });
};

Coders will now be able to call your new plugin, like so:

$("div p").debug();

There are a couple of very important points to remember:

  • All new functions are attached to the $.fn object.
  • 'this' is a reference to the $() object.
  • Any functions you attach must have a semicolon (;) at the end of the function - otherwise the code may bork during compression.
  • Your function must return the $() object, unless explicity noted otherwise.
  • You should use this.each to interate over the current set of matched elements - it produces clean, compatible, code that way.

If you need to write any private methods (ones that the end-coder doesn't need to see), you can feel free to bind them to the $ object itself, for example:

$.test = function() {
  // Do some internal stuff
};

You can then access it in the same manner:

$.test("some stuff");

We recommend that you do it this way so that your code doesn't accidentally overwrite any other functions that the user may be using.

If you're curious as to how a full module looks, feel free to Browse the Source Code