[英]Keeps saying result property is not defined. Why?
Here is my object and I have defined all the properties and functions but it still gives me this error result is not defined
. 这是我的对象,我已经定义了所有的属性和函数,但它仍然给我这个错误
result is not defined
。
Here is my code 这是我的代码
var Xml = {
to : null,
from : null,
url : null,
result : null, //<--- I defined result here
init: function (fromaddress, toaddress, link) {
from = fromaddress;
to = toaddress;
url = link;
this.requestXml();
return this;
},
requestXml: function () {
$.ajax({
type: "GET",
url: url,
dataType: "xml",
success: this.parseXml
});
},
parseXml: function (xml) {
console.log('xml: ' + $(xml));
result = $(xml); //<--- Assigning value to result here
},
getResult: function () {
console.log('Result: ' + result); // <--- Here is says result is not defined
return result;
}
};
How can I solve this problem? 我怎么解决这个问题?
Update 更新
I am calling getResult() below 我在下面调用getResult()
var Route = {
fromurl : null,
tourl : null,
from : null,
to : null,
init: function (fromaddress, toaddress) {
from = fromaddress;
to = toaddress;
fromurl = 'http://demo.com/url'+fromurl;
tourl = 'http://demo.com/url'+tourl;
Route.searchRoute();
},
searchRoute: function () {
var xml = Xml.init(from, to, fromurl);
console.log(xml.getResult()); //<---- calling getResult();
}
};
The "undecorated" expression result
would refer to a global variable named result
, which you do not have. “未修饰”的表达式
result
将引用一个名为result
的全局变量,这是您没有的。
It is not correct to assume that just because a reference to result
is textually inside of an object that the reference refers to a property of that object. 假设仅仅因为对
result
的引用在文本上位于对象内部而引用引用该对象的属性是不正确的。 That may be the case in other languages, but not in JavaScript. 在其他语言中可能就是这种情况,但在JavaScript中则不然。
The solution to your problem is in one of the comments to the question. 您问题的解决方案是对问题的评论之一。 Using
this.
用
this.
as a prefix works in these cases. 作为前缀适用于这些情况。 Try this code:
试试这段代码:
var x = 1;
var p = {
x: 2,
f: function () {alert(x); alert(this.x);}
}
p.f();
Here you will see 1 alerted and then 2. 在这里你会看到1提醒然后2。
Answer to updated question 回答更新的问题
What you have here is a classic problem. 你在这里有一个经典的问题。 You write
你写
var xml = Xml.init(from, to, fromurl);
console.log(xml.getResult()); //<---- calling getResult();
The first line eventually fires of an Ajax request. 第一行最终触发了Ajax请求。 Once that request is fired off, you immediately go to your second line, where you call
xml.getResult()
. 一旦该请求被触发,您立即转到第二行,在那里调用
xml.getResult()
。 Chances are nearly 100% that the call to getResult
will happen before your Ajax call is able to fill in the value of result
. 在您的Ajax调用能够填充
result
值之前,对getResult
的调用将会发生几乎100%的result
。
One approach is to pass the thing you want to do with the result to the init
method. 一种方法是通过你想要的结果对我们该做
init
方法。 In this case it looks like you want to log the result, so try 在这种情况下,您似乎想要记录结果,请尝试
var xml = Xml.init(from, to, fromurl, function () {console.log(xml.getResult()});
Here we have a new fourth parameter to Xml.init
so we have to handle that by updating the Xml
object, like so (not tested): 这里我们有一个新的第四个参数给
Xml.init
所以我们必须通过更新Xml
对象来处理它,就像这样(未经测试):
.
.
.
init: function (fromaddress, toaddress, link, callback) {
from = fromaddress;
to = toaddress;
url = link;
this.requestXml(callback);
return this;
},
requestXml: function (callback) {
$.ajax({
type: "GET",
url: url,
dataType: "xml",
success: callback
});
},
.
.
.
In other words, when you are going to make asynchronous calls, consume the results right away. 换句话说,当您打算进行异步调用时,立即使用结果。 Don't save them away for later, because you never know when they are going to be "ready".
不要把它们保存起来以备日后使用,因为你永远都不知道它们什么时候会“准备好”。
You should use this
, but it might be easier if you use a constructor so you have access to the properties in all methods: 您应该使用
this
,但如果您使用构造函数可能会更容易,因此您可以访问所有方法中的属性:
function Xml(to, from, url, result) {
this.to = to || null;
this.from = from || null;
this.url = url || null;
this.result = result || null;
// init logic
}
Xml.prototype = {
parseXml: function (xml) {
console.log('xml: ' + $(xml));
this.result = $(xml);
},
getResult: function () {
console.log('Result: ' + this.result);
return this.result;
}
...
}
var xml = new Xml(to, from, url, result); // init
Edit: An object literal might not be the best option. 编辑:对象文字可能不是最佳选择。 Your case is more suited for a constructor like above, or a module pattern if you want to avoid the
this
confusion: 您的情况更适合上面的构造函数,或者如果您想避免
this
混淆,则更适合模块模式:
function Xml() {
var xml = {}
, to = null
, from = null
, url = null
, result = null;
xml.parseXml = function( xml ) {
...
};
...
return xml;
}
var xml = Xml();
try XML.result
尝试
XML.result
See Understanding "this" in Javascript for a discussion of the ins and outs of this
and what it points to a different times. 请参阅了解“本”在Javascript为来龙去脉的讨论,
this
和它指向一个不同的时代内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.