Keeps saying result property is not defined. Why?

前端 未结 3 1138
一生所求
一生所求 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();
    

提交回复
热议问题