3

Possible Duplicate:
In jQuery, what does $.fn. mean?

Can someone explain the code below a little more ?

I see that a function called isdirty is defined. What I don't get is:

  1. What is $.fn ?
  2. The second line calls the function on a selector. The function now seems a method of the object I selected ??? Can i attach this function to all objects like this?

    (function($) {
        $.fn.isdirty = function(settings) {
            alert(test);
    }};
    
    $('.dirtycheck').isdirty();
    
Community
  • 1
  • 1
HerbalMart
  • 1,669
  • 3
  • 27
  • 50
  • Although you've probably figured out from the answers below, to explicitly answer your second question - yes, you can extend any jQuery object using this technique. – chrisfrancis27 May 18 '11 at 11:19

2 Answers2

7
  1. $.fn is a shortcut to jQuery.prototype. When you augment the prototype, all jQuery objects will have access to that new method.
  2. Any plugin created like this can access the selected set with this. The general form is...

$.fn.abc = function() {
    // Return it so we can keep chaining.
    return this.each(function() {
       // Do whatever.
    });
};
alex
  • 479,566
  • 201
  • 878
  • 984
3

$.fn === $.prototype

It's the prototype of jQuery. If you extend it you can use it as a method on a jQuery object.

Any object created with the jQuery method (i.e. $("#foo")) will inherit all methods defined on $.fn

So this means you can call $("#foo").isDirty();

Raynos
  • 166,823
  • 56
  • 351
  • 396