jQuery扩展插件和拓展函数的写法(匿名函数使用的典型例子)

      这些年,javascript火起来了,主要归功于AJAX的推广应用,Web2.0的发展。。。于是,出现了很多的javascript框架。我选择了jQuery,最主要是它的思想“write less,do more",因为我是一个挑剔的人,以前写过的代码,会时不时翻出来,看看有没有可以精简,优化的地方。一来是对不断学习的推动,二来可以将新的思想,技术应用到里面去。 

      对于jQuery插件的写法,以前就有介绍过,网上也有很多例子。 这里简要地进行些写法,主要是简写的说明,见下列代码:

 

 <script type="text/javascript" src="jquery-1.4.2.js"></script>


     < script  type ="text/javascript" >
    
        
// jQuery插件的写法(需要传入操作对象)
        ;( function ($)
        {
            
// PI_TestPlugIn为插件名称,也是插件的操作对象
             // 为了不会与其它插件名重复,这里我使用PlugIn的缩写PI_来定义插件对象前缀
            $.fn.PI_TestPlugIn =    
            {
                
// 该插件的基本信息
                Info:{
                    Name: 
" TestPlugIn " ,
                    Ver: 
" 1.0.0.0 " ,
                    Corp: 
" Lzhdim " ,
                    Author: 
" lzhdim " ,
                    Date: 
" 2010-01-01 08:00:00 " ,
                    Copyright: 
" Copyright @ 2000-2010 Lzhdim Technology Software All Rights Reserved " ,
                    License: 
" GPL "
                },
                
// 具有对象参数的函数,这里参数是一个对象,具有属性
                FunctionWithParams: function (paramObj)
                {
                    
// 使用参数,是否使用默认值
                     var  params  =  paramObj  ?  paramObj :  new   function (){
                                                           param1
=   " 1 " ;
                                                           param2
=   " 2 " ;
                                                       };
                    
                                                   
                    
return   this .Info.Name  +   " .FunctionWithParamObject " ;
                },
                
// 具有参数的函数,这里参数是一个变量
                FunctionWithParam: function (varparam)
                {
                    
// 使用参数,是否使用默认值
                     var  param  =  varparam  ?  varparam :  null ;
                                                   
                                                   
                    
return   this .Info.Name  +   " .FunctionWithParam " ;
                },
                
// 不具有参数的函数
                FunctionWithOutParam: function ()
                {
                    
return    this .Info.Name  +   " .FunctionWithOutParam " ;
                }
            };
        })(jQuery);
        
        
        
// jQuery拓展函数的写法(不需要传入操作对象),即API函数
        ;( function ($)
        {
            $.extend({
                
// FN_TestExtendFunction为拓展函数的操作对象
                 // 为了不会与其它插件名重复,这里我使用Extend的缩写FN_来定义函数对象前缀
                FN_TestExtendFunction:
                {
                    
// 该拓展函数的基本信息
                    Info:{
                        Name: 
" TestExtendFunction " ,
                        Ver: 
" 1.0.0.0 " ,
                        Corp: 
" Lzhdim " ,
                        Author: 
" lzhdim " ,
                        Date: 
" 2010-01-01 08:00:00 " ,
                        Copyright: 
" Copyright @ 2000-2010 Lzhdim Technology Software All Rights Reserved " ,
                        License: 
" GPL "
                    },
                     // 具有对象参数的函数,这里参数是一个对象,具有属性
                    FunctionWithParams: function (paramObj)
                    {
                        
// 使用参数,是否使用默认值
                         var  params  =  paramObj  ?  paramObj : {
                                                           param1: 
" 1 " ,
                                                           param2: 
" 2 "
                                                       };
                                                   
                                                   
                        
return   this .Info.Name  +   " .FunctionWithParamObect " ;
                    },
                    
// 具有参数的函数,这里参数是一个变量
                    FunctionWithParam:  function  (varparam) {
                        
// 使用参数,是否使用默认值
                         var  param  =  varparam  ?  varparam :  null ;


                        
return   this .Info.Name  +   " .FunctionWithParam " ;
                    },
                    
// 不具有参数的函数对象
                    FunctionWithOutParam: function ()
                    {
                        
return   this .Info.Name  +   " .FunctionWithOutParam " ;
                    }
                }
            });
        })(jQuery);



        $(
function  () 
        {
            
// 测试插件
             var  params  =  
            {
                param1: 
" 3 " ,
                param2: 
" 4 "
            };
            
            alert($(
this ).PI_TestPlugIn.FunctionWithParams(params));

            alert($.FN_TestExtendFunction.FunctionWithOutParam());
        });
        
        
    
</ script >

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lzhdim

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值