[英]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.