Skip to content

Instantly share code, notes, and snippets.

@lanqy
Last active December 15, 2015 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lanqy/5252872 to your computer and use it in GitHub Desktop.
Save lanqy/5252872 to your computer and use it in GitHub Desktop.
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