node - this.func()不是函数

Ayu*_*ISM 2 javascript scope prototype this node.js

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;
Run Code Online (Sandbox Code Playgroud)

这里的Javascript新手.当我创建一个Job对象并调用该performTask方法时,我得到"that.task不是一个函数".不应该this在方法的最开头performTask提到Job?我犯的错是什么?另外,有没有更好的方法来做我想做的事情?

Fel*_*ing 7

我犯的错是什么?

您正在使用箭头功能.this内部箭头函数的解析方式不同,它不会引用您的Job实例.

不要将箭头函数用于原型方法.

看看Arrow函数vs函数声明/表达式:它们是等价/可交换的吗?欲获得更多信息.