javascript插件的几种写法
来源:互联网 发布:淘宝哪个店卖高仿aj好 编辑:程序博客网 时间:2024/06/15 08:30
一、js原生插件的写法
(1)工厂模式
var Helloword = function(objId){ var _get_dom = function(Id){ return document.getElementById(Id); } var _aim_obj = _get_dom(objId); var _say_hello = function(str){ _aim_obj.innerHTML = str; HelloWorld.sayHello('hello','hello text'); } return{ sayHello:_say_hello }}
var Hei = new Helloword('hello');Hei.sayHello('Hello Word');var Hei2 = new Helloword('hi');Hei2.sayHello('hi');
由调用我们可以看出,每调用一次就要重新示例化一次,这样次数少了还好,加入一百次,一千次,占用内存不说,还相当繁琐,如此我们应该想到让内存中保存一份就够了
(2)自执行函数
;var plugin =(function(){ function _firstFunc(str){ console.log(str); }; return{ firstFunc: _firstFunc, };})();
<script type="text/javascript"> plugin.firstFunc("Hello ! I am firstFunc"); </script>
这样看起来很方便,当然也是我很喜欢的一种插件写法
(3)面向对象,prototype原型模式
//自定义类 function plugin(){}//提供默认参数plugin.prototype.str = "default param";//提供方法(如果不传参,则使用默认参数)plugin.prototype.firstFunc = function(str = this.str){ alert(str);}//创建"对象"var p = new plugin();//调用方法p.firstFunc("Hello ! I am firstFunc");//Hello ! I am firstFuncp.firstFunc();//default param
二、jQuery插件写法
(1)对JQuery自身的扩展插件
这种插件是对JQuery自身的方法库进行扩展的。在使用的时候通过$.MethodName()的方式直接使用。
$.extend({ handleTableUI: function(table){ varthisTable = $("#" +table); $(thisTable).find("tr").bind("mouseover",function () { $(this).css({color: "#ff0011", background:"blue" }); }); $(thisTable).find("tr").bind("mouseout",function () { $(this).css({color: "#000000", background:"white" }); }); }});
<scripttype="text/javascript"> $(document).ready(function() { $.handleTableUI("newTable"); });</script>
(2)对HTML标记或页面元素进行扩展
使用这种插件的扩展方式,在使用此插件时,需要首先引用经过JQuery包装的页面元素,如:$("#tableId").Method()。
(function ($) { $.fn.setTableUI= function(options){ var defaults = { evenRowClass:"evenRow", oddRowClass:"oddRow", activeRowClass:"activeRow" } var options = $.extend(defaults, options); this.each(function(){ varthisTable=$(this); $(thisTable).find("tr").bind("mouseover",function () { $(this).css({color: "#ff0011", background:"blue" }); }); $(thisTable).find("tr").bind("mouseout",function () { $(this).css({color: "#000000", background:"white" }); }); }); };})(jQuery);
<script type="text/javascript"> $(document).ready(function() { $("#newTable").setTableUI(); });</script>
(3)对HTML标记或页面元素进行扩展
不要用在页面显式调用JQuery的方法,而是通过直接添加JQuery插件脚本引用,即可实现对该插件的调用。
一般,如果需要用到一些全局的JQuery插件,即:插件的方法不需要显式调用,而是引用脚本即可;同时,这种插件一般对整个Web页面起到全局配置或设置的作用,如:对<body></body>内部的内容进行整体布局,此时可以采用脚本引用的方式实现。
插件代码示例:
(function ($) { $.tableUI= { set: function (){ varthisTable = $("table"); $(thisTable).find("tr").bind("mouseover",function () { $(this).css({color: "#ff0011", background:"blue" }); }); $(thisTable).find("tr").bind("mouseout",function () { $(this).css({color: "#000000", background:"white" }); }); } }; //此处需要进行自调用 $(function() { $.tableUI.set(); });})(jQuery);
示例说明:如果上面这段代码在my.plugin.js文件中,那么,我们只需要在页面上添加对此脚本文件的引用即可,引用方式为:<scriptsrc="Scripts/my.plugin.js"type="text/javascript"></script>,当然,在所有要用到JQuery的地方,需要首先添加对JQuery库脚本的引用。在引用型插件的代码中,最主要的就是在插件中要主动调用自己所写的插件方法,上面代码中有注释的地方。否则,我们写的插件代码将不会起作用。
特别提醒,该博文有些代码以及说明来自大神博文,说声抱歉,在这里更多的是对于插件的一种整合与记忆。
阅读全文
1 0
- javascript插件的几种写法
- javascript 的几种写法
- javascript的几种写法
- jQuery插件的几种写法
- javaScript插件的写法
- 几种javascript的改进写法
- javascript数组的几种写法 []
- JavaScript类的几种写法
- 简洁的几种javascript写法
- JavaScript函数的常见几种写法
- Javascript数组中迭代的几种写法
- JavaScript函数的几种写法
- Javascript 函数的几种写法
- JavaScript函数的几种写法
- jQuery插件的几种写法大总结
- asp.net Javascript 的几种写法与提示
- [javascript]js函数的几种写法 闭包概要
- JavaScript中创建命名空间的几种写法(推荐)
- JavaScript小细节
- Makefile文件怎么写
- 机器学习实战(4)朴素贝叶斯学习笔记_20170707
- ios美颜 调研 GPUImage GPUImageBeautifyFilter BeautifyFaceDemo
- Linux 设备驱动之 UIO 用户态驱动优缺点分析
- javascript插件的几种写法
- ViewPager+Fragment 多层嵌套问题,第二次进入显示空白
- JAVA中service实现类中的@Service(demoService)是什么意思?
- Hive中java对数据库、表的操作
- 人脸检测——生成矫正人脸——cascade cnn的思想, 但是mtcnn的效果貌似更赞
- docker 远程连接
- JPA入门
- 116. Populating Next Right Pointers in Each Node
- 点滴学习Linux---- mail命令发送邮件