0

我有以下 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 中检查控制台时,我得到以下异常:"Uncaught TypeError: Object [object Object] has no method 'XMLHttpRequest'",所以我假设它没有识别正确的引用,但是,我可以当我放“。”时,看到所有属性/方法都存在。在控制台中,但我无法从内部/外部方式访问(这是评论父构造函数条件)。谢谢,我会等你的回复。

4

0 回答 0