1 插件对象级别开发
var Beautifier = function(ele, opt) {
this.$element = ele
this.settings= ele.data(key);
if(typeof this.settings == "undefined"){
this.defaults = {
'color': 'red',
'fontSize': '12px',
'textDecoration':'none'
},
this.settings= $.extend({}, this.defaults, opt)
}else{
this.settings= $.extend({}, this.settings, opt)
}
ele.data(key,settings);
}
Beautifier.prototype = {
init: function() {
return this.$element.each(function(){
var a = this.$element.data("key");
});
}
}
$.fn.myPlugin = function(options) {
var beautifier = new Beautifier(this, options);
return beautifier.beautify();
}
$("#supper").myPlugin(opt).init();
$("#supper").myPlugin({}).init();
2.函数式开发
(function($) {
var privateFunction = function() {
}
var methods = {
val: function(){
},
init: function(list,opt) {
return this.each(function() {
var $this = $(this);
var settings = $this.data("key");
if(typeof settings == "undefined"){
var default ={
name: "aa",
myList: list
}
settings = $.extend(true,default,opt);
}else{
settings = $.extend(true,settings,opt);
}
$this.data("key",settings);
});
},
destroy: function() {
return this.each(function() {
this.removeData("key")
});
}
};
$.fn.pluginName = function() {
var method = arguments[0];
if(methods[method]) {
method = methods[method];
} else if( typeof(method) == 'object' || !method ) {
method = methods.init;
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.pluginName' );
return this;
}
return method.call(this);
}
})(jQuery);
$("#supper").pluginName(list,opt)
$("#supper").pluginName({},{})