I'm not very familliar with jQuery and it's functions. Does anyone know what these declarations are doing? (Btw. they wrap the entire .js content)
(function ($) { 'use strict' ... })(jQuery);
(function () { 'use strict' ... })();
The second I guess, is a declaration of an anonymus function to not make variables inside accessable from outside.
I know there's a ready function that is called when the DOM was loaded.
$(function () { 'use strict' ... });
Though I can't figure out what the first 2 functions do.
They are self-invoking functions, protecting the scope.
Note how the parameter (jQuery) is accepted as $ in the first function. In this way you can use the short-syntax everywhere, while still operating in a non-conflict mode.
(function ($) { 'use strict' ... })(jQuery);
This will make $
available only in the scope of the self calling anonymous function, this means that $
will not pollute the global scope and it will make sure that $
is jQuery
. If there were other frameworks setting their own $
(like prototype), inside the function closure $
will be jQuery
because jQuery
is the parameter passed in that will be named and available inside the function as $
. Local scope variables in JavaScript have precedence over parent scope.
(function () { 'use strict' ... })();
This is a self calling anonymous function acting as a closure, usually to keep variables scoped locally and not leak them to the global scope.
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