3

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?

4
  • 1
    You're trying to call .percentage() on res, not on this.done. Try doing res.percentage = function() ...; return res;. Commented Jul 17, 2015 at 22:56
  • 1
    this.done must return this, not res. Commented Jul 17, 2015 at 22:57
  • 3
    But really, rethink this a bit. You probably don't want to put presentation logic in the function tracking progress. Commented Jul 17, 2015 at 22:57
  • Why processTrack = new function()? You can do processTrack = function() {} and use variables inside function scope, exposing only an object returned by the function. Commented Jul 17, 2015 at 22:59

2 Answers 2

2

You need to return this instead of res at the end of your this.done function. By returning this, you are returning your this.done function, which is the object that has the 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();
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, so that was the problem. Thanks.
1

Return this in done method. As it is returning res which is not an object

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.