js静态方法与实例方法定义,js回调方法定义

主要为了回调方法,随便把静态言法和实例方法也回顾一下。

<script type="text/javascript">

        var fun = {
            //下面是静态方法(第一种方式)
            f1: function () {
                alert("这是静态方法 f1()");
            },
            f2:function(callback){
                alert("这是静态方法 f2()");
                if(callback && (callback  instanceof Function)){ 
                    callback();//回调方法
                }  
            }
        }

        var c = function () {
            //实例方法
            this.f1=function(){
                alert("这是实例方法 c.f1()");
            }
        }
        //下面是静态方法(第二种方式)
        c.f2 = function () {
            alert("这是静态方法 c.f2()");
        }

        callbackfun = function () {
            alert("这是定义好的回调方法!");
        }

        $(function () {
            var node = document.getElementById('test');
            node.onclick = function () {
                //调用静态方法
                fun.f1();
                //调用回调方法
                fun.f2(function () { alert("这是回调方法!");});
                fun.f2(callbackfun);

                //调用静态方法
                c.f2();
                //调用实例方法
                var c1 = new c();
                c1.f1();
                //c1.f2();//这样不能调用会报错


                //alert(typeof (fun));//obj
                //alert(typeof (fun.f1));//function
                //alert(typeof(c.f2)); //function
                //var c1 = new c();
                //alert(typeof (c));//function
                //alert(typeof(c1));//object
                //alert(typeof(c1.f1));//function
            }            
        });

    </script>
原文地址:https://www.cnblogs.com/q149072205/p/3396095.html