2
function Job(name, cronString, task) {
    "use strict";

    this.name = name;
    this.cronString = cronString;
    this.isReady = false;
    this.task = task;
}

Job.prototype.performTask = (db, winston) => {
     "use strict";
    const Promise = require("bluebird");
    let that = this;
    return new Promise((resolve, reject) => {
        let output = "";
        let success = true;

        try {
            output = that.task();
        }
        catch(error) {
            success = false;
            reject(error);
        }

        if(success) {
            resolve(output);
        }
    });
};

module.exports = Job;

Javascript newbie here. When I make a Job object and call the performTask method, I get "that.task is not a function". Shouldn't this at the very beginning of the performTask method refer to Job? What is the mistake I'm making? Also, is there a better way of doing what I'm trying to do here?

AyushISM
  • 381
  • 7
  • 21
  • And where is your `Job.prototype.task` function? – Overv May 09 '16 at 16:18
  • @Overv It's passed in in the constructor function. – James Thorpe May 09 '16 at 16:18
  • 1
    Possible duplicate of [Arrow function vs function declaration / expressions: Are they equivalent / exchangeable?](http://stackoverflow.com/questions/34361379/arrow-function-vs-function-declaration-expressions-are-they-equivalent-exch) – user2864740 May 09 '16 at 16:25

1 Answers1

7

What is the mistake I'm making?

You are using an arrow function. this inside arrow functions is resolved differently, it won't refer to your Job instance.

Do not use arrow functions for prototype methods.

Have a look at Arrow function vs function declaration / expressions: Are they equivalent / exchangeable? for more information.

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143