简体   繁体   English

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

[英]JavaScript “is not a function” error when calling defined method

This is my code: 这是我的代码:

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. 从别的其他函数调用相同的该死的函数工作正常。

You could do this, right before you call the XML generation. 您可以在调用XML生成之前执行此操作。

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. 因为在使用新函数更改范围时经常会发生this ,所以无法使用它来访问原始值。 Aliasing it to that allows you still to access the original value of this. 将其别名为允许您仍然可以访问此原始值。

One important piece of the puzzle that is missing is how response_xml is being called. 缺少的一个重要部分是如何调用response_xml This is important, because it will change what this is (see Jared's comment). 这很重要,因为它会改变this一点(见Jared的评论)。

Remember that this can be thought of as (roughly) "the receiver of the method call". 请记住, this可以被认为是(大致)“方法调用的接收者”。 If response_xml is passed directly to use as a callback then of course it won't work -- this will likely be window . 如果直接传递response_xml以用作回调,那么当然它将不起作用 - this可能是window

Consider these: 考虑这些:

var x = {f: function () { return this }}
var g = x.f
x.f() === x    // true
g() === x      // false
g() === window // true

Happy coding. 快乐的编码。


The "fix" is likely just to change how response_xml is being called. “修复”可能只是改变了response_xml的调用方式。 There are numerous ways to do this (generally with a closure). 有很多方法可以做到这一点(通常有一个闭包)。

Examples: 例子:

// 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()
}

Personally, I would just use jQuery or similar ;-) 就个人而言,我只会使用jQuery或类似的东西;-)

If you want a class-like behavior, use the right syntax, The libraries that use that, are using JSON to pass a parameter to a function that makes a class out of it. 如果你想要类似于类的行为,请使用正确的语法,使用它的库,使用JSON将参数传递给一个使其成为类的函数。

function MyClass(CTOR paarams){
    var 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();
            }

        }
    }

    var peter_save_data=function()
    {
       // removed function code
    }
}

var Test = new MyClass(somthing,another_something);
Test.response_xml();
//etc etc.

Or, use the libraries like Mootools where you can do it as JSON: 或者,使用像Mootools这样的库,您可以将其作为JSON:

var T = new Class({
    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
    }

});
var X = new T();//etc etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误的JavaScript方法未定义使用PHP调用 - error javascript method is not defined calling using php 在侦听器函数内调用方法时引用错误(Javascript) - Reference error when calling a method inside a listener function (Javascript) Javascript错误,明确定义时function未定义 - Javascript error, function not defined when it is clearly defined 调用扩展字符串对象中定义的JavaScript函数获取错误对象没有方法 - calling JavaScript function defined in an extended String objects gets error object has no method 在javascript函数的指令中定义的控制器中调用方法 - Calling a method in a controller defined in a directive from a javascript function 如何解决从 JavaScript 调用函数到 Node.js 时未定义错误 require() - How to resolve error require() is not defined when calling a function from JavaScript to Node.js 在JavaScript中调用嵌套函数-在jQuery.ajax.success中调用时未定义错误 - Calling Nested Function In Javascript - Is Not Defined Error When Called Within jQuery.ajax.success 调用javascript原型方法时未定义的函数 - undefined function when calling javascript prototype method 错误:调用另一个函数返回的函数时未定义函数 - Error: function is not defined when calling a function returned by another function 在JavaScript中调用对象方法时,不是函数TypeError - Not a Function TypeError when Calling Object Method in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM