学习总结:jQuery插件开发模式和结构

学习博客链接:

https://www.cnblogs.com/cyStyle/

② https://www.cnblogs.com/chengyunshen/p/7277305.html

③ https://www.cnblogs.com/sjqq/p/6410839.html

jQuery插件的开发包括两种:

(1)类级别的插件开发,即给jQuery添加新的全局函数,jQuery的全局函数就是属于jQuery命名空间的函数,相当于给jQuery类本身添加方法。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgwAAAAXCAYAAABnCBd+AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAz+SURBVHhe7Z1NbBRHFoDf7gEhDjE/x+wuB8aAEQwCKwg2CHEzDgjMBQ4IZTiBOJhoFYEvcIgvhOyBcIjYkwdFPuA9gCMS23uKCAkIyUYZIszPIGTMHg1Yu0KIy+573fVm3pSruqrbM+MJrk8qTVfVq/fqp+tnqqtn/vDy9X//B79jli5doq4CgUAgEAg0ij+qz0AgEAgEAgErYcEQCCwyLqXYlbPJJumgOF/3vpO1jFnSNao+56O3GW3cqLoiGZOcb5iNNLI26qEjC2HBEAgsAuo9wPS+fZeok+Jd7n3EVc+NGuhJbz3rNGs+s5RfDyO/zdULqiuTPmmrUfepzbaOj0yzWXRnGMZOLIGDxWNw7e0/oEuFBZrMs+OwrNQBpQOfQU4FVShfhJ2fAhR/NsQZKcOljzfAw7Pv4Js9KiiBhWl/yuNRgCu3odevUA2BB0H+9CFJ1haXZqCz6X6M7TSK7bQH22mtCvu9IOvFdu3CV9anrn1t6nAe0uSbkPK2ax1bnI9tHxmGZG2QDpeupHzqsL60SP1J9pLy2ShaZMEwBbev3oIXyvfBhr3QtWm58iVTnwVDPOn0jSsvLL4FxdiPS6C/7QHc2tKEGc26YBiBk0sPAFz3m/xjWmPBQHr7O7D+TlnqbxTL3AMtcV/5DDZZBkDWqet3+U2YFwxluINtfVf001ZZUCTViw1XfdpIU5c+de3CR8d8y2HDZjvJHsmb4lmPrlP6bfYIm05TGpueJP0uT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SXWkcZ6eZ52FlMmH5CTqPIqqOWrONHKTnhNRHAZzKptN5hc7ceVpllHGj10BqYFi3D5v5kk4SosZXFVGvigR8SYeWH9UGla35Ek2QYQ02uJ4TUh8FCNBL3xd1zJ2nVUY5Z3oNJGF9pmOIcgB87WYcRVQ9tszj1mXnsymnzSZR9dQDf24HSc8M1a5BTaYGxisE5RgvkuY34uftGhAaD6cAspX6Q6Quxhc55pMAN5NsDKsxRaetQbnVZoboxptte3pZz5+3E6rP9lpiFQRBEISYyHuQgiAIguBAHKQgCIIgOBAHKQiCIAgbAPg/sMpHpILeSgcAAAAASUVORK5CYII=" alt="" />

大佬的总结经验(第一个链接):

(1)把全部代码放在闭包中(闭包相当于一个私有作用域,外部无法访问到内部的信息,并且不会存在全局变量的污染情况)

(2)提供插件的默认参数选项

(3)遍历多个元素并返回(Sizzle选择器引擎,链式调用)

(4)一次性代码放在主循环外

(5)定义公有方法和私有方法(增加代码的健壮性)

(function($) {
// 在插件容器内,定义一个私有方法
var privateFunction = function() {
// code here
}; // 通过字面量创造一个对象,存储我们需要的共有方法
var methods = {
// code here
}; })(jQuery);

(6)添加持久性数据($.data函数为元素添加信息),$.removeData函数来删除相应的数据

上一篇:android listview 异步加载图片并防止错位


下一篇:Mysql中的外键分析(什么是外键,为什么要用外键,添加外键,主外键关联删除)