I read that if you do this, you can avoid conflicts with other libraries that use $ symbol.
(function($) {
// jQuery code
})(jQuery)
Could someone explain it to me, please?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You define an anonymous function with an argument named
$. Then you call it immediately thereafter with thejQueryobject (whose name is more than likely explicit enough to not collide with some other library object’s name).Thus within the scope of the anonymous function (where all your jQuery code will be),
$will refer to thejQueryobject. And hence everyone is happy: your code gets to use the convenient shorthand$and code later on can use$for their own business.