【发布时间】:2011-05-27 22:11:22
【问题描述】:
jQuery 网站列出了 jQuery 的基本插件语法,如下所示:
(function( $ ){
$.fn.myPlugin = function() {
// there's no need to do $(this) because
// "this" is already a jquery object
// $(this) would be the same as $($('#element'));
this.fadeIn('normal', function(){
// the this keyword is a DOM element
});
};
})( jQuery );
我只是想从 Javascript 的角度了解那里发生了什么,因为它看起来不像我以前见过的 JS 的任何语法。所以这是我的问题清单:
-
如果将 function($)... 替换为变量,例如“the_function”,则语法如下所示:
(the_function)( jQuery );什么是“(jQuery);”正在做? the_function 周围的括号真的有必要吗?他们为什么在那里?有没有其他类似的代码可以提供?
它以函数 ($) 开头。所以它正在创建一个函数,据我所知,它永远不会运行,使用已经定义的 $ 参数?那里发生了什么?
感谢您的帮助!
【问题讨论】:
标签: javascript jquery jquery-plugins