Sunday, May 13, 2012

jquery - difference between $.functionName and $.fn.FunctionName


In jQuery, I have seen both the following ways of defining a jQuery function:




$.fn.CustomAlert = function() {
alert('boo!');
};

$.CustomAlert = function() {
alert('boo!');
};



I understand that they are attached to the jQuery object (or $), but what is the difference between the two? When should I use one or the other?



Thanks.


Source: Tips4all

1 comment:

  1. A

    $.a = function() {
    return "hello world";
    };

    alert($.a());


    B

    $.fn.b = function() {
    return "hello " + $(this).length + " elements";
    }

    alert($("p").b());

    ReplyDelete