jQuery扩展插件和拓展函数的写法

<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({
                //PIF_TestExtendFunction为拓展函数的操作对象
                //为了不会与其它插件名重复,这里我使用PlugIn的缩写PI_来定义插件对象前缀
                PIF_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($.PIF_TestExtendFunction.FunctionWithOutParam());
        });
        
        
    </script>

上一篇:BZOJ 3675 [Apio2014]序列分割 (斜率优化DP)


下一篇:ubuntu16.04 装了一天的gitlab