调用定义的方法时,JavaScript"不是函数"错误

Rya*_*yan 18 javascript javascript-events

这是我的代码:

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
},
Run Code Online (Sandbox Code Playgroud)

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

Error: this.peter_save_data is not a function
Run Code Online (Sandbox Code Playgroud)

从别的其他函数调用相同的该死的函数工作正常.

Ani*_*han 30

您可以在调用XML生成之前执行此操作.

var that = this;
Run Code Online (Sandbox Code Playgroud)

然后...

that.peter_save_data();
Run Code Online (Sandbox Code Playgroud)

由于this在使用新函数更改范围时经常更改,因此无法使用它来访问原始值.将其别名为允许您仍然可以访问此原始值.

  • 我会考虑一个可能不必要的全局变量,它可能很难调试(例如,如果它已被其他东西使用或在其他地方设置,并且在方法完成时没有清除). (2认同)

小智 6

缺少的一个重要部分是如何 response_xml被召唤.这很重要,因为它会改变this现状(参见Jared的评论).

请记住,this可以将其视为(大致)"方法调用的接收者".如果response_xml直接传递用作回调,那么当然它将无法工作 - this很可能window.

考虑这些:

var x = {f: function () { return this }}
var g = x.f
x.f() === x    // true
g() === x      // false
g() === window // true
Run Code Online (Sandbox Code Playgroud)

快乐的编码.


"修复"可能只是改变response_xml被调用的方式.有很多方法可以做到这一点(通常有一个闭包).

例子:

// Use a closure to keep he object upon which to explicitly invoke the method
// inside response_xml "this" will be "that",
// which was "this" of the current scope
http_request.onreadystatechange = (function (that) {
   return function () { return that.response_xml() }
}(this)

// Or, alternatively,
// capture the current "this" as a closed-over variable...
// (assumes this is in a function: var in global context does not create a lexical)
var self = this
http_request.onreadystatechange = function () {
   // ...and invoke the method upon it
   return self.response_xml()
}
Run Code Online (Sandbox Code Playgroud)

就个人而言,我只会使用jQuery或类似的东西;-)