I’m reading a book on JavaScript that’s explaining how to use the MVC pattern with JavaScript applications. In this code, it’s showing how to set up a controller. Can anyone explain why the jQuery dollar sign is passed as a parameter to the function in line3, and then why there’s no dollar sign in front of (jQuery) in the last line?
var Controller = {};
// Use a anonymous function to enscapulate scope
(Controller.users = function($){
var nameClick = function(){
/* ... */
};
// Attach event listeners on page load
$(function(){
$("#view .name").click(nameClick);
});
})(jQuery);
There’s no dollar sign in front of
jQueryin the last line because the name of the variable that holds jQuery isjQuery, not$jQuery. It’s passed immediately as$to avoid naming conflicts.