Skip to main content

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: Tips4allCCNA FINAL EXAM

Comments

  1. I'm sure this question has been asked several times before, but I can't find the link.

    $.fn points to the jQuery.prototype. Any methods or properties you add to it become available to all instance of the jQuery wrapped objects.

    $.something adds a property or function to the jQuery object itself.

    Use the first form $.fn.something when you're dealing with DOM elements on the page, and your plugin does something to the elements. When the plugin has nothing to do with the DOM elements, use the other form $.something.

    For example, if you had a logger function, it doesn't make much sense to use it with DOM elements as in:

    $("p > span").log();


    For this case, you'd simply add the log method to the jQuery object iself:

    jQuery.log = function(message) {
    // log somewhere
    };

    $.log("much better");


    However, when dealing with elements, you would want to use the other form. For example, if you had a graphing plugin (plotGraph) that takes data from a <table> and generates a graph - you would use the $.fn.* form.

    $.fn.plotGraph = function() {
    // read the table data and generate a graph
    };

    $("#someTable").plotGraph();


    On a related note, suppose you had a plugin which could be used either with elements or standalone, and you want to access it as $.myPlugin or $("<selector>").myPlugin(), you can reuse the same function for both.

    Say we want a custom alert where the date is prepended to each alert message. When used as a standalone function, we pass it the message as an argument, and when used with elements, it uses the text of the element as the message:

    (function() {
    function myAlert(message) {
    alert(new Date().toUTCString() + " - " + message);
    }

    $.myAlert = myAlert;

    $.fn.myAlert = function() {
    return this.each(function() {
    myAlert($(this).text());
    });
    };
    })();


    It's wrapped in a self-executing function so myAlert doesn't spill out to the global scope. This is an example or reusing functionality between both the plugin forms.

    As theIV mentioned, it is a good practice to return the jQuery wrapped element itself since you wouldn't want to break chaining.

    Finally, I found similar questions :-)


    http://stackoverflow.com/questions/1991126/difference-jquery-extend-and-jquery-fn-extend
    http://stackoverflow.com/questions/2671734/jquery-plugin-fn-question
    http://stackoverflow.com/questions/538043/jquery-plugin-authoring-why-do-some-do-jquery-pluginname-and-others-jquery-fn-pl
    http://stackoverflow.com/questions/2398007/in-jquery-what-is-the-difference-between-myfunction-and-fn-myfunction

    ReplyDelete
  2. A

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

    alert($.a());


    B

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

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

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.