将原型插件移植到 jQuery。
该插件使用禁止的方法收集对象文字中的所有插件方法,然后像 [object].[method] 一样调用它们
我不明白的是,在任何这些方法中,都使用了属性(在脚本的请求中定义,即 var x = 0、var y = 0 等),这些属性似乎是全局的,而不是作为参数或属性传递的一种具体的方法。
我将如何在 jQuery 中做到这一点,这可能吗?
请参考下面代码中的“var1”。这将在哪里设置,以便所有方法都可以访问它?
例子:
;(function($){
var methods = {
init : function(options) {
var config = {
// default options...
}
// overide config
var settings = $.extend(config, options);
return this.each(function() {
// init code goes here...
});
},
function1 : function() {
function2();
},
function2 : function() {
$(selector).css({
width : var1,
});
},
}
$.fn.[PLUGINNAME] = 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);