Keeps saying result property is not defined. Why?

前端 未结 3 1097
一生所求
一生所求 2021-01-23 08:16

Here is my object and I have defined all the properties and functions but it still gives me this error result is not defined.

Here is my code



        
相关标签:
3条回答
  • 2021-01-23 08:40

    You should use this, but it might be easier if you use a constructor so you have access to the properties in all methods:

    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:

    function Xml() {
    
      var xml = {}
        , to = null
        , from = null
        , url = null
        , result = null;
    
      xml.parseXml = function( xml ) {
        ...
      };
    
      ...
    
      return xml;
    
    }
    
    var xml = Xml();
    
    0 讨论(0)
  • 2021-01-23 08:51

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

    0 讨论(0)
  • 2021-01-23 09:01

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

    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.

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

    Here you will see 1 alerted and then 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. 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.

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

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

    0 讨论(0)
提交回复
热议问题