继承XMLHttpRequest / ActiveXObject类的问题

时间:2013-05-04 20:10:13

标签: javascript oop inheritance xmlhttprequest activexobject

我有以下JavaScript类:

var Server = function(onError)
{
    /* public As, onError; */

    var that, Key, Headers;

    this.__construct = function()
    {
        that = this;

        that.As = false;
        that.onError = onError;
        that.resetHeaders();

        onError = null;

        // Here I try to call the parent constructor (it seems I can't).

        if(window.XMLHttpRequest)
            that.XMLHttpRequest();
        else
            that.ActiveXObject('Microsoft.XMLHTTP');
    }

    this.Request = function(File, PostData, Function)
    {
        var Method, HeaderKey;

        if(PostData == null)
            Method = 'GET';
        else
            Method = 'POST';

        try
        {
            that.open(Method, File, that.As);

            /* Each request sets X-Requested-With to XMLHttpRequest by default.
               If PostData is given, then we treat it's content type as a form.*/

            that.setRequestHeader('X-Requested-With', 'XMLHttpRequest');

            if(PostData != null)
                that.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

            for(HeaderKey = 0; HeaderKey < Headers.length; HeaderKey++)
                that.setRequestHeader(Headers[ HeaderKey ].Name, Headers[ HeaderKey ].Value);

            if(Function != null)
                that.onreadystatechange = function()
                {
                    if(that.readyState == 4 && that.status == 200)
                        Function.call();
                }

            that.send(PostData);
        }
        catch(Exception)
        {
            if(that.onError != null)
                that.onError(Exception);
        }
    }

    this.addHeader = function(Name, Value)
    {
        Headers[ Key ] = {};
        Headers[ Key ].Name = Name;
        Headers[ Key ].Value = Value;

        Key++;
    }

    this.resetHeaders = function()
    {
        Headers = [];
        Key = 0;
    }

    this.__construct();
}

if(window.XMLHttpRequest)
    Server.prototype = new XMLHttpRequest();
else
    Server.prototype = new ActiveXObject('Microsoft.XMLHTTP');

Server.prototype.constructor = Server;

我根据window.XMLHttpRequest var的状态进行继承。在__construct方法中,我再次重新检查它以调用父构造函数。

我不知道这是不是正确的形式,但我希望有人能告诉我这里有什么问题。顺便说一句,当我在Chrome中检查控制台时,我得到以下异常:“未捕获TypeError:对象[对象对象]没有方法'XMLHttpRequest'”,所以我假设它没有识别但是,正确的参考,我可以看到,当我把“。”时,所有的属性/方法都存在。在控制台中,但我无法从内部/外部方式访问(这是注释父构造函数条件)。谢谢,我等你的回复。

0 个答案:

没有答案