保持说没有定义结果属性。为什么? [英] Keeps saying result property is not defined. Why?

查看:70
本文介绍了保持说没有定义结果属性。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的对象,我已经定义了所有属性和函数,但它仍然给我这个错误结果未定义

Here is my object and I have defined all the properties and functions but it still gives me this error 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()

I am calling getResult() below

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

 };


推荐答案

未修饰表达式结果将引用一个名为 result 的全局变量,这是你没有的。

The "undecorated" expression result would refer to a global variable named result, which you do not have.

假设仅仅因为对 result 的引用在文本上是在引用引用该对象的属性的对象内部是不正确的。在其他语言中可能就是这种情况,但在JavaScript中则不然。

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. That may be the case in other languages, but not in JavaScript.

问题的解决方案之一是问题的评论之一。在这些情况下,使用 this。作为前缀。试试这段代码:

The solution to your problem is in one of the comments to the question. Using 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();

在这里你会看到1提醒,然后是2。

Here you will see 1 alerted and then 2.

回答更新的问题

这里有一个经典问题。你写

What you have here is a classic problem. You write

var xml = Xml.init(from, to, fromurl);
console.log(xml.getResult()); //<---- calling getResult();

第一行最终会触发Ajax请求。一旦该请求被触发,您立即转到第二行,在那里您调用 xml.getResult()。在之前 c> getResult 的调用将在之前发生 c> 你的Ajax调用能够填写的值结果

The first line eventually fires of an Ajax request. Once that request is fired off, you immediately go to your second line, where you call 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.

一种方法是将你想要的东西传递给 init 方法。在这种情况下,您似乎想要记录结果,请尝试

One approach is to pass the thing you want to do with the result to the init method. 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()});

这里我们有一个新的第四个参数 Xml.init 所以我们必须通过更新 Xml 对象来处理它,就像这样(不是测试):

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):

.
.
.
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".

这篇关于保持说没有定义结果属性。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆