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>

 

上一篇:Linux获取ip失败


下一篇:从零开始安装Hadoop视频教程