gpt4 book ai didi

javascript - jQuery 插件创作和命名空间

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:35:55 24 4
gpt4 key购买 nike

我习惯这样写插件:

;(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);

我知道它并不完美,这就是为什么我现在正在尝试正确学习命名空间、更好的插件结构/模式。不幸的是,我读过的过去几本书逐字逐句地引用了 jQuery 插件创作教程,所以没有太大帮助。该教程似乎将所有内容分开,并且没有显示一个很好的组合示例,这就是我感到困惑的原因。在教程中,它显示了命名空间示例。

jQuery 插件命名空间教程

(function( $ ){
var methods = {
init : function( options ) {
},
show : function( ) {
},
hide : function( ) {
},
update : function( content ) {
}
};

$.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' );
}
};
})( jQuery );
// calls the init method
$('div').tooltip();

我了解结构以及如何访问命名空间对象,但是它显示了不包括任何命名空间的默认值/选项的另一个示例......因此,为了编写一个正确命名空间的插件的开头,具有默认值/选项并缓存我插入的 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);
  1. 显然,当我构建/插入 myElem 时,它只能在该方法内使用,而不能在任何其他方法内使用……我是不是在错误的地方构建了它?

  2. 默认/扩展是否在正确的位置?

  3. 如果我不想从插件外部访问方法,我是否需要方法逻辑部分?

  4. 使用 .prototype 与 .fn 有什么好处吗?

非常感谢任何人和所有人! :)

最佳答案

更仔细地查看“工具提示”示例插件。这是一个真正伟大的模式。

它完成了您需要的所有命名空间,并且已经是您习惯的类型,至少底部的通用“主管” block 是 - 即这部分:

$.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 是直截了当的 javascript 术语中的私有(private)变量,但它的属性由主管以非常聪明、非常规的方式公开为插件的方法。

请不要尝试将默认值/选项代码移出 init 方法。这会把一切都搞砸!遵循久经考验且值得信赖的模式,一切都会好起来的。

编辑:

一定要遵守模式的其他方面:

  • 为了保持 jQuery 对象的可链接性,在每个不返回特定结果的方法中使用 return this.each(function(){...}) 结构。
  • (通常),在 init 中,建立一个 .data('pluninName', {...}) 对象来容纳在初始化时建立的任何数据,以及需要访问的数据/稍后由其他插件方法修改/增强。

该模式只为插件本身提供了一个闭包(包含方法对象);闭包的命名空间不能用于特定于元素的数据(包括初始化选项),因此需要使用 .data('pluninName', ...)

这些不仅仅是约定——它们绝对是使模式按预期工作的关键。

关于javascript - jQuery 插件创作和命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12632382/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com