Reputation: 698
Please help me understand the difference between the below code:
(function() {
//-----some code-----
}());
and:
(function($){
//-----some code-----
})(jQuery);
Thanks!
Upvotes: 4
Views: 56
Reputation: 5399
The second is called no-conflict mode. This means if other frameworks are using the $
sign to mean something else other then the jQuery object, it will not conflict with it. The reason for this is because you are specifying that the $
that you mean is the jQuery
that you pass into the function call!
Remember, a Javascript function can be run immediately like this.
(function(){
})();
So popping in the last pair of brackets [the function call] the jQuery
puts the jQuery
object in the function that runs immediately.
Upvotes: 4