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

Posted 求知若渴 虚心若愚

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>

以上是关于jQuery扩展插件和拓展函数的写法的主要内容,如果未能解决你的问题,请参考以下文章

jQuery开发插件的两个方法 js 深浅拷贝

jQuery的扩展方法写法

jQuery 插件拓展

关于拓展jQuery功能插件的写法

jQuery插件扩展与多库共存

一天一点新东西-jq插件写法