<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>