The jQuery site lists the basic plugin syntax for jQuery as this:
(function( $ ){ $.fn.myPlugin = function() { // there's no need to do $(this) because // "this" is already a jquery object // $(this) would be the same as $($('#element')); this.fadeIn('normal', function(){ // the this keyword is a DOM element }); }; })( jQuery );
I'd just like to understand what is going on there from Javascript's point of view, because it doesn't look like it follows any syntax I've seen JS do before. So here's my list of questions:
If you replace function($)... with a variable, say "the_function", the syntax looks like this:
(the_function)( jQuery );
What is "( jQuery );" doing? Are the parenthesis around the_function really necessary? Why are they there? Is there another piece of code you can give that is similar?
It begins with function( $ ). So it's creating a function, that as far as I can tell will never be run, with the parameter of $, which is already defined? What is going on there?
Thanks for the help!
A jQuery plugin is simply a new method that we use to extend jQuery's prototype object. By extending the prototype object you enable all jQuery objects to inherit any methods that you add. As established, whenever you call jQuery() you're creating a new jQuery object, with all of jQuery's methods inherited.
In jQuery, the $ sign is just an alias to jQuery() , then an alias for a function. This page reports: Basic syntax is: $(selector).action() A dollar sign to define jQuery.
function(x){ x... }
is just a function without a name, that takes one argument, "x", and does things with x.
Instead of 'x', which is a common variable name, you can use $, which is a less common variable name, but still legal.
function($){ $... }
I'll put it in parentheses to make sure it parses as an expression:
(function($){ $.... })
To call a function, you put () after it with a list of arguments. For example, if we wanted to call this function passing in 3 for the value of $
we would do this:
(function($){ $... })(3);
Just for kicks, let's call this function and pass in jQuery as a variable:
(function($){ $.... })(jQuery);
This creates a new function that takes one argument and then calls that function, passing in jQuery as the value.
WHY?
WHY NOT JUST WRITE $ = jQuery
?
(function( $ ){ })( jQuery );
That is self-executing anonymous function that uses $
in argument so that you can use it ($
) instead of jQuery
inside that function and without the fear of conflicting with other libraries because in other libraries too $
has special meaning. That pattern is especially useful when writing jQuery plugins.
You can write any character there instead of $
too:
(function(j){ // can do something like j.fn.function_name = function(x){}; })(jQuery);
Here j
will automatically catch up jQuery specified at the end (jQuery)
. Or you can leave out the argument completely but then you will have to use jQuery
keyword all around instead of $
with no fear of collision still. So $
is wrapped in the argument for short-hand so that you can write $
instead of jQuery
all around inside that function.
If you even look at the source code of jQuery, you will see that everything is wrapped in between:
(function( window, undefined ) { // jQuery code })(window);
That is as can be seen also a self-executing anonymous function with arguments. A window
(and undefined
) argument is created and is mapped with global window
object at the bottom (window)
. This is popular pattern these days and has little speed gain because here window
is will be looked into from the argument rather than global window
object which is mapped below.
The $.fn
is jQuery's object where you create your new function (which is also an object) or the plugin itself so that jQuery wraps your plugin in its $.fn
object and make it available.
Interestingly, I had answered similar question here:
JavaScript / jQuery closure function syntax
You can also check out this article to know more about self-executing functions that I had written:
Javascript Self-executing Functions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With