JavaScript“不是一个功能”。调用定义的方法时出错 [英] JavaScript "is not a function" error when calling defined method

查看:71
本文介绍了JavaScript“不是一个功能”。调用定义的方法时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

request_xml: function()
        {
        http_request = false;
                    http_request = new XMLHttpRequest();
                     if (http_request.overrideMimeType) 
                            {
                            http_request.overrideMimeType('text/xml');
                            }
                          if (!http_request)
                          {
                                return false;
                          }
                        http_request.onreadystatechange = this.response_xml;
                        http_request.open('GET', realXmlUrl, true);
                        http_request.send(null);
                        xmlDoc = http_request.responseXML;

},



response_xml:function ()
    {
        if (http_request.readyState == 4)
        {
            if(http_request.status == 404 && countXmlUrl<=3)
            {
                countXmlUrl++;

                realXmlUrl = xmlUrl[countXmlUrl];
                this.request_xml();
            }
            if (http_request.status == 200)
            {
                xmlDoc = http_request.responseXML;
                alert("need to update3");
                this.peter_save_data();
            }

        }
    },

peter_save_data:function()
    {
// removed function code
},

奇怪的是,警报触发没有问题但是下面的函数调用给了我这个错误:

Strangely, the alert fires without a problem but the function call underneath gives me this error:

Error: this.peter_save_data is not a function

从其他地方调用同样该死的函数可以正常工作。

Calling the same damn function from another function elsewhere works fine.

推荐答案

你在你调用XML代之前就可以做到这一点。

You could do this, right before you call the XML generation.

var that = this;

及更高版本......

and later...

that.peter_save_data();

因为这个经常在改变范围时改变使用新功能,您无法使用它来访问原始值。将其别名为允许您仍然可以访问此原始值。

Because this frequently changes when changing scope by using a new function, you can't access the original value by using it. Aliasing it to that allows you still to access the original value of this.

这篇关于JavaScript“不是一个功能”。调用定义的方法时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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