Jai*_*Jai 1 javascript jquery closures jquery-plugins
如果我有一个使用正常标准的jQuery插件:
(function( $ ){
var methods = {
init : function( options ) {
var defaults = {
}
var options = $.extend(defaults, options);
return this.each(function(){
var returnValue = myUniversalFunction();
});
},
test : function( options ) {
var defaults = {
}
var options = $.extend(defaults, options);
return this.each(function(){
var returnValue = myUniversalFunction();
});
}
};
$.fn.jPlugin = function( method ) {
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.tooltip' );
}
};
})( jQuery );
Run Code Online (Sandbox Code Playgroud)
我将在哪里放置一个可以在init和test方法中访问但不能在插件本身外部使用的函数?
把它放在第2行,就在(function( $ ){这之后,就像这样:
(function( $ ){
var inner_function = function() {
// ...
};
var methods = {
// ...
};
$.fn.jPlugin = function( method ) {
// ...
};
})( jQuery );
Run Code Online (Sandbox Code Playgroud)
该功能inner_function可在任何地方使用,(function($){ ... })(jQuery);但不在其外部.
| 归档时间: |
|
| 查看次数: |
105 次 |
| 最近记录: |