立诚勿怠,格物致知
It's all about connecting the dots

jQuery plugin design recommendations

Reference: Learning jQuery (Fourth Edition), Chapter 8

Protect the dollar ($) alias from potential interference from other libraries by using jQuery insstead or passing $ into an immediately invoked frunction expression (IIFE), so that it can be used as a local variable.

Whether extending the jQuery object with $.myPlugin or the jQuery prototype with $.fn.myPlugin, add no more than one property to the $ namespace. Additional public methods and properties should be added to the plugin’s namespace (for example, $.myPlugin.publicMethod or $.fn.myPlugin.pluginProperty).

Provide and object containing default options for the plugin: $.fn.myPlugin.defaults = {size: ‘large’}.

Allow the plugin user to optionally override any of the default settings for all subsequent calls to the method ($.fn.myPlugin.defaults.size = ‘medium’;) or for a single call ($(‘div’).myPlugin({size: ‘small’});).

In most cases when extending the jQuery prototype $.fn.myPlugin, return this to allow allow the plugin user to chain additional jQuery methods to it (for example, $(‘div’).myPlugin().find(‘p’).addClass(‘foo’)).

When extending the jQuery prototype ($.fn.myPlugin), enforce implicit iteration by calling this.each().

Employ callback functions when appropriate to allow for flexible modification of the plugin’s behavior without having to change the plugin’s code.

If the plugin calls for user interface elements or needs to track elements’ state, create it with the jQuery UI widget factory.

Maintain a set of automated unit tests for the plugin with a testing framework such as QUnit to ensure that it works as expected.

Use a version control system such as Git to track revisions to the code. Consider hosting the plugin publicly on GitHub (http://github.com/) and allowing others to contribute.

If making the plugin available for others to use, make the licensing ters clear. Consider using the MIT license, which jQuery also uses.

赞(0) 打赏
文章名称:《jQuery plugin design recommendations》
文章链接:https://www.orzzone.com/jquery-plugin-design-recommendations.html
商业联系:yakima.public@gmail.com

本站大部分文章为原创或编译而来,对于本站版权文章,未经许可不得用于商业目的,非商业性转载请以链接形式标注原文出处。
本站内容仅供个人学习交流,不做为任何投资、建议的参考依据,因此产生的问题需自行承担。

评论 抢沙发

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力提供更多优质内容!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册