1、为每一个DOM对象创建一个插件对象

模板定义:

 (function($) {

     $.pluginName = function(element, options) {

         var defaults = {
foo: 'bar',
onFoo: function() {}
} var plugin = this; plugin.settings = {} var $element = $(element),
element = element; plugin.init = function() {
plugin.settings = $.extend({}, defaults, options);
// code goes here
} plugin.foo_public_method = function() {
// code goes here
} var foo_private_method = function() {
// code goes here
} plugin.init(); } $.fn.pluginName = function(options) {
return this.each(function() {
//为每一个DOM元素创建插件实例
if (undefined == $(this).data('pluginName')) {
var plugin = new $.pluginName(this, options);
$(this).data('pluginName', plugin);
}
}); } })(jQuery);

模板使用:

 $(document).ready(function() {

     // 将插件附加到选择器中的每一个元素(这里通过ID选择器,只有一个元素,下同。)
$('#element').pluginName({'foo': 'bar'}); // 隐式迭代,选择器中每一个元素都会调用该公共方法
$('#element').data('pluginName').foo_public_method(); // 隐式迭代,访问选择器中每一个元素的属性,返回一个数组
$('#element').data('pluginName').settings.foo; });

2、面向对象的模板,只有一个插件实例

模板定义:

 ;(function($) {

     $.pluginName = function(el, options) {

         var defaults = {
propertyName: 'value',
onSomeEvent: function() {}
} var plugin = this; plugin.settings = {} var init = function() {
plugin.settings = $.extend({}, defaults, options);
plugin.el = el;
// code goes here
} plugin.foo_public_method = function() {
// code goes here
} var foo_private_method = function() {
// code goes here
} init(); } })(jQuery);

模板使用:

 $(document).ready(function() {

     // 创建插件实例,并且绑定到$('#element')
var myplugin = new $.pluginName($('#element')); // 调用插件公共方法
myplugin.foo_public_method(); // 获取公有属性的值
myplugin.settings.property; });

3、面向对象的模板,充分利用习惯的链式编程

模板定义:

 ; (function ($) {
//构造函数
$.pluginName = function (el, options) { var defaults = {
propertyName: 'value',
onSomeEvent: function () { }
} var plugin = this; plugin.settings = {} var init = function () {
plugin.settings = $.extend({}, defaults, options);
plugin.el = el;
// code goes here
}
//①:直接方法定义(模板2就是采用这种方式)
plugin.foo_public_method = function () {
//公有方法
// code goes here
} var foo_private_method = function () {
//私有方法
// code goes here
} init(); }
//②:原型方法定义(均为公有方法)
$.pluginName.prototype = {
method1: function () {
},
method2: function () {
}
}; //③:原型方法定义 也可以这么写(均为公有方法)
//$.extend($.pluginName, {
// method1: function () {
// },
// method2: function () {
// }
//}); //在插件中使用
$.fn.pluginName = function (options) {
//实例化一个插件实例,通过执行构造函数初始化
var myPlugin = new $.pluginName(this, options);
//调用公有业务方法
myPlugin.method1();
myPlugin.foo_public_method();
//返回 this,便于链式调用
return this;
} })(jQuery);

模板使用:

 $(document).ready(function () {
//熟悉的链式编程
$('#element').pluginName({
//插件options
}).css({}).animate({});
});

4、构造函数提供给外部使用(有点别扭)

模板定义:

 ; (function ($) {
//构造函数
$.pluginName = function (el, options) {
//去除构造函数中对插件的初始化,转到$.fn.pluginName中初始化。
return $(el).pluginName(options);//该构造函数不是给插件使用,而是给外部调用者使用,需要return以链式编程
}
//②:原型方法定义(均为公有方法)
$.pluginName.prototype = {
method1: function (para1, para2) {
},
method2: function (para1, para2) {
}
}; //③:原型方法定义 也可以这么写(均为公有方法)
//$.extend($.pluginName, {
// method1: function (para1,para2) {
// },
// method2: function (para1,para2) {
// }
//}); //在插件中使用,不会创建插件实例(构造函数是给外部使用的)
$.fn.pluginName = function (options) {
var defaults = {
propertyName: 'value',
onSomeEvent: function () { }
} var settings = {} var init = function () {//私有方法
settings = $.extend({}, defaults, options);
// code goes here
} var foo_private_method = function () {
//私有方法
// code goes here
} init(); //调用业务方法
$.pluginName.method1(para1, para2);
$.pluginName.method2(para1, para2); //返回 this,便于链式调用
return this;
} })(jQuery);

模板使用:

 $(document).ready(function () {
//熟悉的链式编程
$('#element').pluginName({
//插件options
}).css({}).show({}); //构造函数提供该外部使用,所以相当于
new $.pluginName($('#element'), {
//插件options
}).css({}).animate({});
});

总结

正如标题所说的那样,每种模板各有特点,但是最具可读性的还是第三种。当然了,模板只是一个套路,修修改改就又会变成另外一种模板了,上面只是总结了比较常见的模板格式,仅供参考。

不错的参考

segmentfault jQuery 模板

jQuery Biilerplate

jQuery 插件模板的更多相关文章

  1. 我最喜欢的jQuery插件模板

    我使用jQuery已经有相当长的时间了,并且我会常常为它写一些插件(plugin).我尝试过用不同的方式去写,现在这个模板是我最喜欢的: 1 2 3 4 5 6 7 8 9 10 11 12 13 1 ...

  2. 关于jquery插件模板的两个案例

    /** * jquery tips 提示插件 jquery.tips.js v0.1beta * * 使用方法 * $(selector).tips({ //selector 为jquery选择器 * ...

  3. JQuery插件模板

    (function($){ $.fn.插件名 = function(settings){ var defaultSettings = { } /* 合并默认参数和用户自定义参数 */settings ...

  4. 简记 jQuery 插件模板

    /** * @lisence jquery plugin demo v1.0.0 * * author: Jeremy Yu * * description: * this is a jquery p ...

  5. jQuery插件编写及链式编程模型小结

    JQuery极大的提高了我们编写JavaScript的效率,让我们可以愉快的编写代码,做出各种特效.大多数情况下,我们都是使用别人开发的JQuery插件,今天我们就来看看如何把我们常用的功能做出JQu ...

  6. jQuery插件编写及链式编程模型

    jQuery插件编写及链式编程模型小结 JQuery极大的提高了我们编写JavaScript的效率,让我们可以愉快的编写代码,做出各种特效.大多数情况下,我们都是使用别人开发的JQuery插件,今天我 ...

  7. 前端模板文件化jQuery插件 $.loadTemplates

    工作中使用前端模板引擎,如 artTemplate.jsRender,来替代拼接字符串. 可是直接把模板写在页面上会带来页面臃肿,模板无法重用,与 ASP.NET等后端语言语法冲突等问题. 所以将多个 ...

  8. 出位的template.js 基于jquery的模板渲染插件

    找了好几款基于jquery的模板渲染插件,无一感觉很难用(教程较少.绑定不统一),也可能我智商问题,比如jquery template.js .jtemplate.js. 然后在github上找到这一 ...

  9. javascript&&jquery编写插件模板

    javascrpt插件编写模板 这里不分享如何编写插件,只留一个框架模板,使用面向对象的形式进行编写,方便管理 ;(function(window,document){ function FnName ...

随机推荐

  1. Meteor错误:TypeError: Meteor.userId is not a function

    问题描述: 浏览器console提示错误TypeError: Meteor.userId is not a function. 原因分析: 通过查看Meteor API文档,可知该函数由包accoun ...

  2. 纯代码添加约束条件(Auto Layout)

    Auto Layout 是一种基于约束的.描述性的布局系统.也就是使用约束条件来描述布局,View的Frame会根据这些描述来进行计算. 在iOS6.0以后加入了一个新类: NSLayoutConst ...

  3. easyui-prompt弹出框操作

    效果图如下: 代码如下: $(document).ready(function () { //绑定按钮操作 $('#btnMove').click(function () { var ids = ge ...

  4. C#多线程同步

    在编写多线程程序时无可避免会碰到线程的同步问题.什么是线程的同步呢? 举个例子:假如在一个公司里面有一个变量记录某人T的工资count=100,有两个主管A和B(即工作线程)在早一些时候拿了这个变量的 ...

  5. OpenJudge 2694 逆波兰表达式

    1.链接地址: http://bailian.openjudge.cn/practice/2694/ 2.题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 逆波兰表达式是一种把运算 ...

  6. 第48条:如果需要精确的答案,请避免使用float和double

    float和double主要为了科学计算和工程计算而设计,执行二进制浮点运算,这是为了在广泛的数值范围上提供较为精确的快速近似计算而精心设计的.然而,它们没有提供完全精确的结果,所以不适合用于需要精确 ...

  7. 实习笔记-2:sql 分组不一定要group by

    今天在公司写代码的时候,遇到一个sql语句构建问题. 情形是这样的: 我需要获取不同小组下前N条记录. select top 10 * from dbo.Topic where GroupID in ...

  8. C#拓展练习之模拟键盘录入

    摘自<31天学会CRM项目开发<C#编程入门级项目实战>> 使用C#调用Windows API使程序模拟键盘输入,也可模拟按下快捷键.本例中,单击“模拟输入”按钮,可录入字符“ ...

  9. QtSQL学习笔记(1)- 概述

    Qt SQL是Qt提供的核心模块,用以支持SQL数据库.Qt SQL的API被分为不同的层: ■ 驱动层 (Driver layer) ■ API层 (SQL API layer) ■ 用户接口层 ( ...

  10. DataGridView添加另外一个控件。

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; u ...