简体   繁体   English

保持说没有定义结果属性。 为什么?

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

相关问题 为什么说未定义回调? - Why is it saying callback is not defined? “未捕获的ReferenceError:未定义失败。”为什么会发生这种情况? - “Uncaught ReferenceError: fail is not defined.” Why is this happening? javascript:递归,方法已定义,但一直说它未定义 - javascript: recursion, method is defined, but it keeps on saying it's not defined 为什么一直说我的对象不是函数? - Why it keeps saying that my object is not a function? React JS Sementic UI 一直说:“ReferenceError: jQuery is not defined” - React JS Sementic UI keeps saying: "ReferenceError: jQuery is not defined" 电子:为什么会出现“未捕获的引用错误:需要未定义。”? - Electron: Why occurs 'Uncaught ReferenceError: require is no defined.'? 代码一直说“未捕获的类型错误:无法设置属性‘值’为空” - Code keeps saying “Uncaught TypeError: Cannot set property 'value' of null” javascript为什么说未定义addcallback函数? - Why is javascript saying that addcallback function is not defined? Require 没有出现在我的代码中,但是 webpack 不断抛出错误“require is not defined”。 - Require doesn't appear in my code, but webpack keeps throwing the error "require is not defined." 为什么Jshint在这个if语句中说“变量已经定义”? - Why is Jshint saying “variable already defined” in this if statement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM