[Javascript] 插件封装

来源:互联网 发布:网络教育 营养学 编辑:程序博客网 时间:2024/06/05 02:51

JavaScript插件封装主要分成两类:

* 类级别封装-相当于jQuery类的静态函数

* 对象级别封装


一.  类级别封装方法

1.单个方法封装

[html] view plain copy
  1. //封装  
  2. jQuery.alert = function(name) {  
  3.     alert(name);  
  4. }  
  5.   
  6. //调用$.alert('aa')或jQuery.alert('aa')  
  7. $.alert('Mike');  

2.多个方法封装,调用$.warn('aa')或者jQuery.warn('aa')

[html] view plain copy
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="/jquery/jquery-1.11.1.min.js">  
  5. </script>  
  6. <script>  
  7. $(document).ready(function(){  
  8.   $("p").click(function(){  
  9.        jQuery.warn('Mike');  
  10.   });  
  11.   
  12. });  
[html] view plain copy
  1. //封装  
  2. jQuery.extend({  
  3.   alert: function(name) {  
  4.       alert("alert:" + name);  
  5.   },  
  6.   warn: function(name) {  
  7.       alert("warn:" + name);   
  8.   }  
  9. });  
  10. </script>  
  11. </head>  
  12. <body>  
  13. <p>如果您点击我,我会消失。</p>  
  14. <p>点击我,我会消失。</p>  
  15. <p>也要点击我哦。</p>  
  16. </body>  
  17. </html>  

3.域名调用,并封装多个方法

[html] view plain copy
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="/jquery/jquery-1.11.1.min.js">  
  5. </script>  
  6. <script>  
  7. $(document).ready(function(){  
  8.   $("p").click(function(){    
  9.        //调用  
  10.        jQuery.messager.warn('Mike');  
  11.   });  
  12.   
  13. });  
  14.   
  15. //封装  
  16. jQuery.messager = {  
  17.   
  18.   alert: function(name) {  
  19.       alert("alert:" + name);  
  20.   },  
  21.   warn: function(name) {  
  22.       alert("warn:" + name);   
  23.   }  
  24.   
  25. }  
  26.   
  27. </script>  
  28. </head>  
  29. <body>  
  30. <p>如果您点击我,我会消失。</p>  
  31. <p>点击我,我会消失。</p>  
  32. <p>也要点击我哦。</p>  
  33. </body>  
  34. </html>  

二. 对象级别的插件开发

形式1:

[html] view plain copy
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="/jquery/jquery-1.11.1.min.js">  
  5. </script>  
  6. <script>  
  7. $(document).ready(function(){  
  8.   $("p").click(function(){   
  9.        //重要:$.extend(obj)扩展的为jQuery类的静态方法调用可采用如$.alert() 或直接alert()  
  10.        //$.fn.extend(obj)扩展的为对象的方法,因此调用不能用$.alert(),要直接alert()  
  11.        //调用  
  12.        alert('Mike');  
  13.        //报错  
  14.        //$.alert('Mike');  
  15.   });  
  16.   
  17. });  
  18.   
  19. //声明一个函数并立即执行函数,用$(形参)代替jQuery实参  
  20. $(function($) {  
  21.     $.fn.extend({  
  22.         alert: function(name) {  
  23.             alert(name);  
  24.         }  
  25.     });  
  26. }(jQuery));  
  27. </script>  
  28. </head>  
  29. <body>  
  30. <p>如果您点击我,我会消失。</p>  
  31. <p>点击我,我会消失。</p>  
  32. <p>也要点击我哦。</p>  
  33. </body>  
  34. </html>  

形式2

[html] view plain copy
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="/jquery/jquery-1.11.1.min.js">  
  5. </script>  
  6. <script>  
  7. $(document).ready(function(){  
  8.   $("p").click(function(){   
  9.        //正确调用方式  
  10.        $('body').jAlert('Mike');  
  11.        //报错  
  12.        //$.jAlert('aa');  
  13.        //报错  
  14.        //jAlert('aa');   
  15.   });  
  16.   
  17. });  
  18.   
  19. //声明一个函数并立即执行函数,用$(形参)代替jQuery实参  
  20. $(function($) {  
  21.     //这种方式使得调用方式为$(element).jAlert('aaa'),其他方式$.jAlert('aa')或者jAlert('aa')都会出错  
  22.     $.fn.jAlert = function(name){  
  23.                 alert(name)  
  24.     }  
  25. }(jQuery));  
  26. </script>  
  27. </head>  
  28. <body>  
  29. <p>如果您点击我,我会消失。</p>  
  30. <p>点击我,我会消失。</p>  
  31. <p>也要点击我哦。</p>  
  32. </body>  
  33. </html>  

3.options控制插件

[html] view plain copy
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="/jquery/jquery-1.11.1.min.js">  
  5. </script>  
  6. <script>  
  7. $(document).ready(function(){  
  8.   $("p").click(function(){   
  9.        //正确调用方式  
  10.        $(this).jAlert();  
  11.   });  
  12.   
  13. });  
  14.   
  15. //声明一个函数并立即执行函数,用$(形参)代替jQuery实参  
  16. $(function($) {  
  17.     $.fn.jAlert = function(options){  
  18.         var defaults = {  
  19.            color: '#fff',  
  20.            background: '#000'  
  21.         };  
  22.       options = $.extend({}, defaults, options);  
  23.        //$(this).css({'color': options['color')});  
  24.     $(this).css({'color': "" + options['color'], 'background': "" + options['background']});  
  25.        
  26.     };   
  27. }(jQuery));  
  28. </script>  
  29. </head>  
  30. <body>  
  31. <p>如果您点击我,我会消失。</p>  
  32. <p>点击我,我会消失。</p>  
  33. <p>也要点击我哦。</p>  
  34. </body>  
  35. </html>  

3.暴露设置、暴露函数、私有设置、私有函数

暴露与暴露函数使得插件可扩展

暴露设置,用户使用时可设置如$.fn.hilight.defaults.color

暴露函数,用户可重写暴露的函数如$.fn.hilight.format = function(){}

有一点注意:

调用插件时$(element).hilight(), 则插件内操作元素如设置样式等,都是针对该element,在插件内通过$(this)获取element,

经过调试,如hilight插件,return this.each(function(...))是指对每个符合要求的element操作,如$('p'),对所有<p>标签操作,如果插件内不用return this.each,也会对标签内的所有<p>操作,但是方法只执行了一次

[html] view plain copy
  1. // 创建一个闭包      
  2. (function($) {      
  3.   // 插件的定义      
  4.   $.fn.hilight = function(options) {      
  5.     debug(this);      
  6.     // build main options before element iteration      
  7.     var opts = $.extend({}, $.fn.hilight.defaults, options);      
  8.     // iterate and reformat each matched element      
  9.     return this.each(function() {      
  10.       $this = $(this);      
  11.       // build element specific options      
  12.       var o = $.meta ? $.extend({}, opts, $this.data()) : opts;      
  13.       // update element styles      
  14.       $this.css({      
  15.         backgroundColor: o.background,      
  16.         color: o.foreground      
  17.       });      
  18.       var markup = $this.html();      
  19.       // call our format function      
  20.       markup = $.fn.hilight.format(markup);      
  21.       $this.html(markup);      
  22.     });      
  23.   };      
  24.   // 私有函数:debugging      
  25.   function debug($obj) {      
  26.     if (window.console && window.console.log)      
  27.       window.console.log('hilight selection count: ' + $obj.size());      
  28.   };      
  29.   // 定义暴露format函数      
  30.   $.fn.hilight.format = function(txt) {      
  31.     return '<strong>' + txt + '</strong>';      
  32.   };      
  33.   // 插件的defaults      
  34.   $.fn.hilight.defaults = {      
  35.     foreground: 'red',      
  36.     background: 'yellow'      
  37.   };      
  38. // 闭包结束      
  39. })(jQuery);       

0 0
原创粉丝点击