Javascript OOP - Function within return function -
trying create function call 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 ideally want call processtrack.done(args).percentage()
percentage data recieved .done(args)
, whenever try call (for example) processtrack.done(true, false).percentage()
gives me error saying:
typeerror: processtrack.done(...).percentage not function
what doing wrong?
you need return this
instead of res
@ end of this.done
function. returning this
, returning this.done
function, object has percentage
function in it.
the following code runs without errors:
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 this; }; this.step = function() { return 1 / (this.max - this.min); }; } processtrack.done(true, false).percentage();
Comments
Post a Comment