zoukankan      html  css  js  c++  java
  • callback调用测试

    <html>
    <head>
    <script>    
        var context="全局";
        var testObj={        
            context:"初始",
            callback:function (str){//回调函数
                alert("callback:我所处的上下文中,context="+this.context+",我被回调的方式:"+str);
            },
            caller:function(){            
                callWithClosure(function(param){this.callback(param);});
                var temp=this;
                callWithClosure(function(param){temp.callback(param);});            
            } 
        };//创建一个对象,作为测试回调函数的上下文
        testObj.context="已设置";
        
        function testCall(){                    
            callMethod(testObj.callback);
            testObj.caller();
            callWithClosure(function(param){testObj.callback(param);});
            callObjMethod(testObj,testObj.callback);        
        } 
        function callObjMethod(obj,method){        
            method.call(obj,"指定显式对象上下文回调");
        }
        function callMethod(method){        
            method("通过默认上下文回调");        
        }
        function callWithClosure(method){        
            method("通过Closure保持上下文回调");        
        }
        
        function callback(str){
            alert("callback:我是定义在外部的全局函数。");
        }
    </script>
    </head>
    <body>
    <a href="javascript:void(0)" onclick="testCall()">调用测试</a>
    </body>
    </html>
  • 相关阅读:
    BEC listen and translation exercise 44
    中译英12
    BEC listen and translation exercise 43
    中译英11
    BEC listen and translation exercise 42
    中译英10
    BEC listen and translation exercise 41
    中译英9
    BEC listen and translation exercise 40
    中译英8
  • 原文地址:https://www.cnblogs.com/blog-leo/p/3917650.html
Copyright © 2011-2022 走看看