这个JavaScript / jQuery语法是什么意思? [英] What does this JavaScript/jQuery syntax mean?
问题描述
以下语法是什么意思?
(function($){
$.fn.columnize = function(options) {
...
什么是函数( $)
?
什么是 $。fn。...
?
推荐答案
编写插件时使用此约定,以确保使用$符号与其他Javascript库没有任何混淆,同时确保插件作者仍可使用这个notataion:
This convention is used when writing plugins to ensure there is no confilict with other Javascript libraries using the $ notation, whilst ensuring the plugin author can still use this notataion:
(function($){
...
})(jQuery);
作者声明一个带有单个参数($)的匿名函数,然后立即调用它并传递它的jQuery对象。这确保调用函数并定义其中的所有内容。
The author is declaring an anonymous function with a single parameter ($), then immediately calling it and passing the jQuery object to it. This ensures the function is called and that everything in it is defined.
更长的符号可能是:
function MyDefs($){
...
}
MyDefs(jQuery);
Altho呃会在全局命名空间中创建一个变量 MyDefs
。匿名函数模式将全局命名空间留空,避免冲突。
Although that would create a variable MyDefs
in the global namespace. The anonymous function pattern leaves the global namespace empty, avoiding conflicts.
这篇关于这个JavaScript / jQuery语法是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!