Skip to content

Instantly share code, notes, and snippets.

@lanqy

lanqy/Plugin_Pattern.js

Last active Dec 15, 2015
Embed
What would you like to do?
jQuery Plugin Pattern
(function($){
$.extend($.fn,{
pluginName: function(options){
return this.each(function(){
var pluginName = $.data(this, "pluginName");
if(!pluginName){
pluginName = new $.pluginName(options, this);
$.data(this, "pluginName", pluginName);
}
});
}
});
$.pluginName = function(options,el){
if(arguments.length){
this.init(options,el) // 初始化
}
};
$.pluginName.prototype = {
a1: "",
a2:"",
options:{
b1:"",
b2:"",
b3:""
},
init: function(options,el){
var o = (this.options = $.extend(true, {}, this.options, options));
this.element = $(el);
this.fn1();
this.fn2();
// do somethin......
},
fn1: function(){
// do somethin......
},
fn2: function(){
// do somethin......
}
};
//对外接口
$.extend($.fn, {
fn1: function(){
// do something...
},
fn2: function(){
// do something...
}
});
})(jQuery);
//
(function($){
$.fn.extend({
pluginName: function(options){
//设置默认值,用逗号隔开
var defaults = {
a1: "",
a2: "",
a3: ""
};
var options = $.extend(defaults, options);
return this.each(function(){
var o = options,$this = $(this);
// do something
})
}
})
})(jQuery)
// pattern 3
(function($){
$.fn.pluginName = function() {
// do something
};
})(jQuery);
// pattern 4
$.fn.pluginName = function() {
// do something
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.