这样的封装方式可以运行:
*********************************
Default.aspx
<input type="button" value="查询" onClick="doQry();"/>
<script src="test.js"></script>
<script language="javascript">function doQry()
{
    var tmpObj=new oAjax();            
tmpObj.send('Default2.aspx',function()
{
if(tmpObj.oSend.readyState==4 && tmpObj.oSend.status==200)
{  
  alert("你好:" + tmpObj.oSend.responseText);
}
else if(tmpObj.oSend.readyState==4 && tmpObj.oSend.status!=200){alert('error');}
})
}
</script>
************************************
test.js
function createXMLHttpRequest() 

    try 
    {         
        if (window.XMLHTTPRequest) return new XMLHttpRequest(); 
        else if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    catch (e) {alert("XMLHttpRequest对象无法创建!请检查IE安全设置!");} 

function oAjax() 

    this.oSend= new createXMLHttpRequest(); 

oAjax.prototype.send=function(url,handler)
{
    this.oSend.open("GET",url,true);   
    this.oSend.onreadystatechange=handler;
    this.oSend.setRequestHeader("If-Modified-Since","0");
    this.oSend.send(null);
}但是我现在在test.js中如果不要this.oSend对象,这样封装,提示出错,请问为什么?
*******************
default.aspx
<input type="button" value="查询" onClick="doQry();"/>
<div id="qryResult"></div>
<script src="test.js"></script>
<script language="javascript">
    var m=new oAjax();
    function doQry() 
    {
     m.send('Default2.aspx',xx); //这里报错,说找不到m.send方法!!!!
    }
    function xx()
    {    }
</script>***********************
test.jsfunction oAjax() 

    return new this.createXMLHttpRequest(); 

oAjax.prototype.createXMLHttpRequest=function() 

    try 
    {         
        if (window.XMLHTTPRequest) return new XMLHttpRequest(); 
        else if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    catch (e) {alert("XMLHttpRequest对象无法创建!请检查IE安全设置!");} 

oAjax.prototype.zz=function(a)
{
  return a;
}
oAjax.prototype.send=function(url,handler)
{
    this.open("GET",url,true);   
    this.onreadystatechange=handler;
    this.setRequestHeader("If-Modified-Since","0");
    this.send(null);
}

解决方案 »

  1.   

    你这样写肯定不行,先看这里://混乱的send方法
    oAjax.prototype.send=function(url,handler){
        this.open("GET",url,true); 
        this.onreadystatechange=handler;
        this.setRequestHeader("If-Modified-Since","0");
        this.send(null);
    }
    //构造函数中return object将覆盖new结果,改变实例的constructor从而无法保持prototype链
    function oAjax(){
        return new this.createXMLHttpRequest();
    } 修正方法:
    function oAjax(){
        this.ajax = new this.createXMLHttpRequest();//将XMLHTTP对象赋给属性
    }oAjax.prototype.send=function(url,handler){//send方法不再混乱
        this.ajax.open("GET",url,true); 
        this.ajax.onreadystatechange=handler;
        this.ajax.setRequestHeader("If-Modified-Since","0");
        this.ajax.send(null);
    }
      

  2.   

    楼上正解,类中必须有个变量保存ajax变量才行.
    不过我自创的下面的写法更容易理解.更接近与C语言的Class
    1.function即class  2.function不能直接嵌套function,必须声明个变量,然后赋值为函数
    test.js
    function Ajax() 

        this.request=function() 
        { 
            try 
            {         
                if (window.XMLHTTPRequest) return new XMLHttpRequest(); 
                else if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP"); 
            } 
            catch (e) {alert("XMLHttpRequest对象无法创建!请检查IE安全设置!");} 
        } 
        this.oAjax= new this.request(); 
        this.send=function(url,handler)
        {
            this.oAjax.open("GET",url,true);   
            this.oAjax.onreadystatechange=handler;
            this.oAjax.setRequestHeader("If-Modified-Since","0");
            this.oAjax.send(null); 
        }
    } test.htm
    <input type="button" value="查询" onClick="doQry();"/>
    <script src="test.js"></script>
    <script language="javascript"> 
        function doQry() 
        {
            var m=new Ajax();
        m.send('default2.aspx',function()
        {
        if(m.oAjax.readyState==4 && m.oAjax.status==200)
        {  
                    alert("你好:" + m.oAjax.responseText);
        }
        else if(m.oAjax.readyState==4 && m.oAjax.status!=200){alert('error');}
        })
    }
    </script>