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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6317845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:14:58  来源:igfitidea点击:

JavaScript "is not a function" error when calling defined method

javascriptjavascript-events

提问by Ryan

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.

从别处的另一个函数调用同一个该死的函数工作正常。

回答by Anirudh Ramanathan

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

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

var that = this;

and later...

然后...

that.peter_save_data();

Because thisfrequently 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.

因为this在使用新函数更改作用域时会频繁更改,所以您无法通过使用它来访问原始值。将其别名为 that 允许您仍然访问 this 的原始值。

回答by Ryan

One important piece of the puzzle that is missing is howresponse_xmlis being called. This is important, because it will change what thisis (see Jared's comment).

缺少的一个重要难题是如何response_xml调用。这很重要,因为它会改变this现状(参见 Jared 的评论)。

Remember that thiscan be thought of as (roughly) "the receiver of the method call". If response_xmlis passed directly to use as a callback then of course it won't work -- thiswill likely be window.

请记住,this可以将其视为(大致)“方法调用的接收者”。如果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_xmlis being called. There are numerous ways to do this (generally with a closure).

“修复”可能只是为了改变response_xml被调用的方式。有很多方法可以做到这一点(通常使用闭包)。

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 或类似的 ;-)

回答by Itay Moav -Malimovka

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