gpt4 book ai didi

javascript - jQuery 插件模板 - 最佳实践、约定、性能和内存影响

转载 作者:IT王子 更新时间:2023-10-29 02:55:19 26 4
gpt4 key购买 nike

我已经开始编写一些 jQuery 插件,并且认为使用 jQuery 插件模板设置我的 IDE 会很好。

我一直在这个网站上阅读一些与插件约定、设计等相关的文章和帖子。我想我会尝试并巩固所有这些。

下面是我的模板,我希望经常使用它,所以很想确保它总体上符合 jQuery 插件设计约定,以及具有多个内部方法(甚至它的一般设计)的想法是否会影响性能并容易发生内存问题。

(function($)
{
var PLUGIN_NAME = "myPlugin"; // TODO: Plugin name goes here.
var DEFAULT_OPTIONS =
{
// TODO: Default options for plugin.
};
var pluginInstanceIdCount = 0;

var I = function(/*HTMLElement*/ element)
{
return new Internal(element);
};

var Internal = function(/*HTMLElement*/ element)
{
this.$elem = $(element);
this.elem = element;
this.data = this.getData();

// Shorthand accessors to data entries:
this.id = this.data.id;
this.options = this.data.options;
};

/**
* Initialises the plugin.
*/
Internal.prototype.init = function(/*Object*/ customOptions)
{
var data = this.getData();

if (!data.initialised)
{
data.initialised = true;
data.options = $.extend(DEFAULT_OPTIONS, customOptions);

// TODO: Set default data plugin variables.
// TODO: Call custom internal methods to intialise your plugin.
}
};

/**
* Returns the data for relevant for this plugin
* while also setting the ID for this plugin instance
* if this is a new instance.
*/
Internal.prototype.getData = function()
{
if (!this.$elem.data(PLUGIN_NAME))
{
this.$elem.data(PLUGIN_NAME, {
id : pluginInstanceIdCount++,
initialised : false
});
}

return this.$elem.data(PLUGIN_NAME);
};

// TODO: Add additional internal methods here, e.g. Internal.prototype.<myPrivMethod> = function(){...}

/**
* Returns the event namespace for this widget.
* The returned namespace is unique for this widget
* since it could bind listeners to other elements
* on the page or the window.
*/
Internal.prototype.getEventNs = function(/*boolean*/ includeDot)
{
return (includeDot !== false ? "." : "") + PLUGIN_NAME + "_" + this.id;
};

/**
* Removes all event listeners, data and
* HTML elements automatically created.
*/
Internal.prototype.destroy = function()
{
this.$elem.unbind(this.getEventNs());
this.$elem.removeData(PLUGIN_NAME);

// TODO: Unbind listeners attached to other elements of the page and window.
};

var publicMethods =
{
init : function(/*Object*/ customOptions)
{
return this.each(function()
{
I(this).init(customOptions);
});
},

destroy : function()
{
return this.each(function()
{
I(this).destroy();
});
}

// TODO: Add additional public methods here.
};

$.fn[PLUGIN_NAME] = function(/*String|Object*/ methodOrOptions)
{
if (!methodOrOptions || typeof methodOrOptions == "object")
{
return publicMethods.init.call(this, methodOrOptions);
}
else if (publicMethods[methodOrOptions])
{
var args = Array.prototype.slice.call(arguments, 1);

return publicMethods[methodOrOptions].apply(this, args);
}
else
{
$.error("Method '" + methodOrOptions + "' doesn't exist for " + PLUGIN_NAME + " plugin");
}
};
})(jQuery);

提前致谢。

最佳答案

不久前,我根据一篇博客文章构建了一个插件生成器:http://jsfiddle.net/KeesCBakker/QkPBF/ .可能会有用。这是相当基本和直接的。任何评论都将非常受欢迎。

您可以创建自己的生成器并根据您的需要进行更改。

附言。这是生成的正文:

(function($){

//My description
function MyPluginClassName(el, options) {

//Defaults:
this.defaults = {
defaultStringSetting: 'Hello World',
defaultIntSetting: 1
};

//Extending options:
this.opts = $.extend({}, this.defaults, options);

//Privates:
this.$el = $(el);
}

// Separate functionality from object creation
MyPluginClassName.prototype = {

init: function() {
var _this = this;
},

//My method description
myMethod: function() {
var _this = this;
}
};

// The actual plugin
$.fn.myPluginClassName = function(options) {
if(this.length) {
this.each(function() {
var rev = new MyPluginClassName(this, options);
rev.init();
$(this).data('myPluginClassName', rev);
});
}
};
})(jQuery);

关于javascript - jQuery 插件模板 - 最佳实践、约定、性能和内存影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5980194/

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