js插件的经典写法与总结

来源:互联网 发布:博克软件 收入 编辑:程序博客网 时间:2024/06/05 01:38

js插件的经典写法与总结


  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>js插件测试</title>  
  5.     <script type="text/javascript" src="./jquery.js"></script>  
  6. </head>  
  7. <body>  
  8. <div style="width:200px;height:200px;border:1px solid;background:red" class="mydiv">测试</div>  
  9. <script type="text/javascript">  
  10. (function($){  
  11.     //定义在闭包函数中的全局变量,用来初始化参数,其他的所有函数可以调用  
  12.     var config;  
  13.   
  14.     //一些私有函数,相当于php类中private的私有方法,被主函数调用  
  15.     var privateFunction = function(){  
  16.           
  17.         // 执行代码  
  18.   
  19.         console.log(arguments[0]);  
  20.     }  
  21.   
  22.   
  23.     //主函数包含在method中,对外暴露,可以被jquery的实例对象执行  
  24.     var methods = {  
  25.         //初始化的函数,传入参数对象  
  26.         init: function(options){       
  27.             // 在每个元素上执行方法,同时返回该jqueryded的实例对象  
  28.             // console.log(options);  
  29.             return this.each(function() {  
  30.                 var $this = $(this);  
  31.                 // console.log($this);  
  32.                 // 尝试去获取settings,如果不存在,则返回“undefined”  
  33.                 var settings = $this.data('pluginName');  
  34.                 // console.log(settings);  
  35.                  // 如果获取settings失败,则根据options和default创建它  
  36.                 if(typeof(settings) == 'undefined'){  
  37.    
  38.                     var defaults = {  
  39.                         name:'zengbing',  
  40.                         sex:'nan',  
  41.                         onSomeEvent: function() {}  
  42.                     };  
  43.    
  44.                     settings = $.extend({}, defaults, options);  
  45.                 // 保存我们新创建的settings  
  46.                     $this.data('pluginName',settings);  
  47.                 }else{  
  48.                     // 如果我们获取了settings,则将它和options进行合并(这不是必须的,你可以选择不这样做)  
  49.                     settings = $.extend({}, settings, options);  
  50.    
  51.                 // 如果你想每次都保存options,可以添加下面代码:  
  52.                     $this.data('pluginName', settings);  
  53.   
  54.                       
  55.                 }  
  56.   
  57.                 //将该配置参数赋值全局变量,方便其他函数调用  
  58.                 config=settings;  
  59.    
  60.                 // 执行私有的方法,完成相关逻辑任务  
  61.                 // privateFunction(config.name);  
  62.    
  63.             });  
  64.         },  
  65.         //销毁缓存的变量  
  66.         destroy: function(options) {  
  67.             // 在每个元素中执行代码  
  68.             return $(this).each(function() {  
  69.                 var $this = $(this);  
  70.    
  71.             // 执行代码  
  72.    
  73.             // 删除元素对应的数据  
  74.                 $this.removeData('pluginName');  
  75.             });  
  76.         },  
  77.   
  78.         //其他的主题函数。可以完成对象的其他操作  
  79.         val: function(options1,options2,options3) {  
  80.         // 这里的代码通过.eq(0)来获取选择器中的第一个元素的,我们或获取它的HTML内容作为我们的返回值  
  81.             var someValue = this.eq(0).html();  
  82.             // 返回值  
  83.             console.log(arguments);  
  84.             return someValue;  
  85.         },  
  86.   
  87.         getContent: function(){  
  88.             return this.each(function(){  
  89.                 var content=$(this).text();  
  90.                 console.log(content);  
  91.                 //获取全局变量的初始化的值  
  92.                 console.log(config.sex)  
  93.             });  
  94.         }  
  95.     };  
  96.    
  97.     $.fn.pluginName = function(){  
  98.         var method = arguments[0];  
  99.         if(methods[method]) {  
  100.             method = methods[method];  
  101.             //将含有length属性的数组获取从第下标为1的之后的数组元素,并返回数组  
  102.             arguments = Array.prototype.slice.call(arguments,1);  
  103.         }else if( typeof(method) == 'object' || !method ){  
  104.             method = methods.init;  
  105.         }else{  
  106.             $.error( 'Method ' +  method + ' does not exist on jQuery.pluginName' );  
  107.             return this;  
  108.         }  
  109.           
  110.         //jquery的实例对象将拥有执行method的能力,method的this是指jquery的实例对象  
  111.         return method.apply(this,arguments);  
  112.    
  113.     }  
  114.    
  115. })(jQuery);  
  116.   
  117. //用法实例  
  118. var config={  
  119.     name:'huang',  
  120.     sex:'nv'  
  121. };  
  122. //先初始化参数配置,在执行各个主体函数,函数中可以调用config的变量,其实就是jquery的链式操作  
  123. $('div.mydiv').pluginName(config).pluginName('getContent').pluginName('val','bing');  
  124.   
  125. </script>  
  126.   
  127. </body>  
  128. </html> 
原创粉丝点击