如何添加回调函数到一个javascript类? [英] How to add callback function to a javascript class?

查看:157
本文介绍了如何添加回调函数到一个javascript类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript中的以下代码会向我显示错误this.callback不是函数

The following code in javascript gives me the error "this.callback is not a function

function ajaxRequest()
{
    var httpObject;

    this.open = open;
    this.callback = function(){};

    function getHTTPObject()
    {
        if (window.ActiveXObject) 
            return new ActiveXObject("Microsoft.XMLHTTP");
        else if (window.XMLHttpRequest)
            return new XMLHttpRequest();
        else 
        {
            alert("Your browser does not support AJAX.");
            return null;
        }
    }

    function onstatechange()
    {
        if(httpObject.readyState == 4)
        {
            this.callback(httpObject.responseText);
        }

    }


    function open(url, callback)
    {
        httpObject = getHTTPObject();
        if (httpObject != null) 
        {
            httpObject.open("GET", url, true);
            httpObject.send(null);
            this.callback = callback;
            httpObject.onreadystatechange = onstatechange;
        }
    }
}

为什么不打开方法将回调参数视为函数?

why doesn't open method treat the callback parameter as function?

如果是,那为什么我不能在onstatechange函数中调用?

If it does then why can't i call it in onstatechange function?

如何使这项工作?

推荐答案

原因是因为 onstatechange 被作为事件处理程序调用,并且 this 指针可能指向事件触发的对象,而不是 ajaxRequest 对象,如你所料。

The reason is that because onstatechange is being called as an event handler, and the this pointer is likely pointing to the object on which the event fired, not the ajaxRequest object, as you expect.

下面的重写将这个变量存储在变量中,在onstatechange()函数可以访问的执行上下文中。这应该解决这个问题。

The below rewrite stores the this variable in a variable called that in the execution context to which the onstatechange() function has access. This ought to cure the problem.

所有这一切的长短都是如果你不完全理解Javascript关闭和执行上下文,即使你做,你'多多,更好的使用框架来做你的AJAX请求。 jQuery和Prototype是很好的选择。

The long and short of all this is if you don't thoroughly understand Javascript closure and execution contexts, and even if you do, you're much, much better off using a framework to do your AJAX requests. jQuery and Prototype are good choices.

function ajaxRequest()
{
    var httpObject;

    this.open = open;
    this.callback = function(){};
    var that = this;

    function getHTTPObject()
    {
        if (window.ActiveXObject) 
                return new ActiveXObject("Microsoft.XMLHTTP");
        else if (window.XMLHttpRequest)
                return new XMLHttpRequest();
        else 
        {
                alert("Your browser does not support AJAX.");
                return null;
        }
    }

    function onstatechange()
    {
        if(httpObject.readyState == 4)
        {
                that.callback(httpObject.responseText);
        }

    }


    function open(url, callback)
    {
        httpObject = getHTTPObject();
        if (httpObject != null) 
        {
                httpObject.open("GET", url, true);
                httpObject.send(null);
                this.callback = callback;
                httpObject.onreadystatechange = onstatechange;
        }
    }
}

这篇关于如何添加回调函数到一个javascript类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆