Javascript jQuery插件编写和名称空间
我习惯于编写这样的插件:Javascript jQuery插件编写和名称空间,javascript,jquery,plugins,javascript-namespaces,Javascript,Jquery,Plugins,Javascript Namespaces,我习惯于编写这样的插件: ;(function($){jQuery.fn.myPlugin=function(options){ var defaults={ 'property':value }, o=$.extend({},defaults,options||{}); // INSERT AND CACHE ELEMENTS var $Element=$('<div></div>'); $Element.ap
;(function($){jQuery.fn.myPlugin=function(options){
var defaults={
'property':value
},
o=$.extend({},defaults,options||{});
// INSERT AND CACHE ELEMENTS
var $Element=$('<div></div>');
$Element.appendTo($('body'));
function funFunction(){
// I have access to $Element!
$Element.hide(500);
};
this.each(function(i){
var $this=$(this);
});
return this;
});};})(jQuery);
我了解该结构以及如何访问名称空间对象,但它显示了默认值/选项的其他示例,不包括任何名称空间。。。因此,为了编写一个具有正确名称空间、具有默认值/选项并缓存我插入的HTML元素以供整个插件使用的插件的开头,我提出了以下建议
正确的组合?
;(function($,window,document,undefined){
var myPlugin={
// METHODS
init:function(options){
},
buildElements:function(){
var $Elements=$('<div id="myElem"></div>')
.appendTo($('body'));
}
};
$.fn.myPlugin=function(method,options){
var defaults={
},
options=$.extend({},defaults,options||{});
myPlugin.buildElements();
return this.each(function(){
var $this=$(this);
if(myPlugin[method]){
return myPlugin[method].apply(this,Array.prototype.slice.call(arguments,1));
}else if(typeof method==='object'||!method){
return myPlugin.init.apply(this,arguments);
}else{$.error('Method '+method+' does not exist on jQuery.myPlugin');};
});
};})(jQuery);
;(函数($,窗口,文档,未定义){
var myPlugin={
//方法
初始化:函数(选项){
},
buildElements:function(){
变量$Elements=$('')
.附于($(“正文”);
}
};
$.fn.myPlugin=函数(方法、选项){
var默认值={
},
options=$.extend({},默认值,options{});
myPlugin.buildElements();
返回此值。每个(函数(){
var$this=$(this);
if(myPlugin[方法]){
返回myPlugin[method].apply(this,Array.prototype.slice.call(arguments,1));
}else if(typeof方法==='object'| |!方法){
返回myPlugin.init.apply(这是参数);
}else{$.error('Method'+Method+'在jQuery.myPlugin上不存在);};
});
};}(jQuery);
$.fn.tooltip = function( method ) {
// Method calling logic
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' );
}
};
methods
在strightforward javascript术语中是一个私有变量,但它的属性以一种非常聪明、非常规的方式被管理者公开为插件的方法
请不要尝试将默认值/选项代码移出init方法。这会把一切都搞砸的!遵循久经考验和信任的模式,一切都会好起来
编辑:
请务必遵守该模式的其他方面:
- 要维护jQuery对象的可链接性,请在每个不返回特定结果的方法中使用
结构返回this.each(function(){…})
- (通常)在init中,建立一个
对象,以容纳初始化时建立的任何数据,以及以后需要通过其他插件方法访问/修改/扩充的数据李>.data('pluniName',{…})
方法对象);闭包的命名空间不能用于特定于元素的数据(包括初始化选项),因此需要使用.data('pluniName',…)
这些不仅仅是约定——它们绝对是使模式按预期工作的关键 非常感谢您的回复!为了确保我理解:将选项/默认值保留在init方法中,然后在fn.name函数中调用它。我将在哪里构建和缓存插件元素,例如实际的工具提示div?谢谢!:)使用此模式,在执行插件代码之后,jquery
有一个新的自定义方法,示例中是“工具提示”。在使用中,插件的调用方式与其他方法相同,例如,$(选择器)代码>但不同的是,插件所需的“内部方法”被指定为第一个字符串参数$(选择器)代码>(默认为“init”)。根据每个“内部方法”的要求,可以传递更多的参数。此架构旨在帮助避免$.fn
中的命名空间冲突,同时允许插件拥有丰富的方法集合。再次感谢您的详细回答,我已经投票并标记为答案:)一旦我解决了这个问题,我会将完整的模板发布到@FabricioMatté,这是我见过的最好的评论
$.fn.tooltip = function( method ) {
// Method calling logic
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' );
}
};