节点 – this.func()不是一个函数

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新手在这里。 当我创build一个Job对象并调用performTask方法时,我得到“that.task不是一个函数”。 不应该在performTask方法的一开始就提到Job ? 我在做什么错误? 另外,有没有更好的方式做我在这里做的事情?

我在做什么错误?

您正在使用箭头function。 this内部的箭头函数parsing不同,它不会引用你的Job实例。

原型方法不要使用箭头函数。

看看箭头函数与函数声明/expression式:它们是等价/可交换的吗? 了解更多信息。