title: Javascript OOP - Function within return function
body: <p>Trying to create function call that only be called if other function has been called within same line.</p>
  
  <pre><code>var processTrack = new function() {
      this.current = 1;
      this.max = 5800;
      this.min = 0;
      this.done = function(started, processing, cur, len) {
          cur = cur || 0;
          len = len || 1;
          var res = 0;
          if (started &amp;&amp; !processing)
              res = ((this.current - 1 - this.min) / (this.max - this.min)).toFixed(2);
          else if (!started &amp;&amp; processing)
              res = (this.done(true, false) + (this.step() * this.cur / this.len)).toFixed(2);
          else if (!started &amp;&amp; !processing)
              res = ((++this.current - 1 - this.min) / (this.max - this.min)).toFixed(2);
  
          this.percentage = function() {
              return res * 100 + "%";
          };
          return res;
      };
      this.step = function() {
          return 1 / (this.max - this.min);
      };
  }
  </code></pre>
  
  <p>What I ideally want is to call <code>processTrack.done(args).percentage()</code> to get percentage from the data I recieved from <code>.done(args)</code>, but whenever I try to call (for example) <code>processTrack.done(true, false).percentage()</code> it gives me an error saying:</p>
  
  <p><code>TypeError: processTrack.done(...).percentage is not a function</code></p>
  
  <p>What am I doing wrong?</p>