Why Need To Pass Jquery Object As Parameter To Closure?
Solution 1:
You don't have to. You can name the function argument whatever you want it to be. It is just a common/best practice of people using jQuery since it is common to use $ as an alias to jQuery library object.
The reason why you should do it is because there are other libraries that use $ as an alias to their library objects. It is needed to avoid collisions with those libraries since function closure will ensure $ to be jQuery object inside the wrapper function.
Here's an example:
(function (myJqueryAlias) {
console.log(myJqueryAlias('document') === jQuery('document'));
})(jQuery);
Solution 2:
With this you archive that in this block you still have $ as jQuery and you can still write stuff like "$("selector")" instead of "jQuery("selector")". And outside this block the variable $ is still what it should be (for example you use another JSLib that uses the $ too)
Post a Comment for "Why Need To Pass Jquery Object As Parameter To Closure?"