Trying to create function call that only be called if other function has been called within same line.
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 && !processing)
res = ((this.current - 1 - this.min) / (this.max - this.min)).toFixed(2);
else if (!started && processing)
res = (this.done(true, false) + (this.step() * this.cur / this.len)).toFixed(2);
else if (!started && !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);
};
}
What I ideally want is to call processTrack.done(args).percentage() to get percentage from the data I recieved from .done(args), but whenever I try to call (for example) processTrack.done(true, false).percentage() it gives me an error saying:
TypeError: processTrack.done(...).percentage is not a function
What am I doing wrong?
.percentage()onres, not onthis.done. Try doingres.percentage = function() ...; return res;.this.donemust returnthis, notres.processTrack = new function()? You can doprocessTrack = function() {}and use variables inside function scope, exposing only an object returned by the function.