What does the following syntax mean?
(function($){
$.fn.columnize = function(options) {
...
What’s function($)
?
What’s $.fn. …
?
This convention is used when writing plugins to ensure there is no confilict with other Javascript libraries using the $ notation, whilst ensuring the plugin author can still use this notataion:
(function($){
...
})(jQuery);
The author is declaring an anonymous function with a single parameter ($), then immediately calling it and passing the jQuery object to it. This ensures the function is called and that everything in it is defined.
A longer notation might be:
function MyDefs($){
...
}
MyDefs(jQuery);
Although that would create a variable MyDefs
in the global namespace. The anonymous function pattern leaves the global namespace empty, avoiding conflicts.
It declares the columnize
function as a jQuery plugin allowing you to use it on elements like this $('.someSelector').columnize()
. You could read more about plugin authoring here.
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