这是我的对象,我已经定义了所有属性和函数,但它仍然给我这个错误result is not defined
。
这是我的代码
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;
}
};
我怎么解决这个问题?
更新
我在下面调用 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();
}
};