对 (function($){ })(jQuery); 的理解
(function($){
...//函数代码块
})(jQuery);
这是一个JS的匿名函数,创建即调用。其中jQuery代表这个函数的实参,$
表示这个函数的形参,所以在函数体内用$代表jQuery。
这种匿名函数的调用形式形成了一个自我执行的封闭程序,jQuery在此程序中映射为$
符号,这样可以避免$号被其他库覆写。
如下的效果是等价的:
!function($){}(JQuery);
~function($){}(JQuery);
例子:
(function(param){
alert(param); //this is a test
})("this is a test");