Can Someone Explain What The Syntax Means When Defining A Jquery Plugin?
Solution 1:
The following parameters with jQuery are just executing the anonymous function and passing in jQuery as the $ parameter. This ensures that $ = jQuery just incase window.$ doesn't equal jQuery.
Here is a rewrite of the code that might make more sense:
functionmyFunc($) {
$.fn.truncate = function() {
returnthis.each(function() {
});
}
myFunc(jQuery);
Solution 2:
The surrounding parentheses create an anonymous function to which the $
symbol references the global jQuery
object.
$.fn.truncate
- This means that you are extending the jQuery
object to include a new function called truncate
.
Usage$( '#someElement' ).truncate();
Solution 3:
It is not safe to assume that $
is owned by the jQuery library. Some other javascript libraries/users may/do use that identifier for other purposes. However, jQuery
is always the jQuery library (barring any evildoers).
This anonymous function handily and safely exposes the $
shortcut as jQuery by making it a local parameter to the anonymous function. Since parameters will override any globals only for the scope of a function this will not affect any other user/library code outside of it.
Finally, where the anonymous function is executed jQuery
is passed in as the first paramter to fill $
.
So when boiled down, this is just a shortcut taken by plugin developers so that they can safely and reliably use $
; if you don't mind using jQuery
everywhere instead then this is totally optional.
Post a Comment for "Can Someone Explain What The Syntax Means When Defining A Jquery Plugin?"