类型错误无法读取 属性 Javascript 函数

Type Error Cannot Read Property Javascript function

我有一个看起来像这样的函数:

function instance(options) {
   this.createInstance = Bluebird.method(function() {
               this.getInstance();
    });

   this.getInstance = Bluebird.method(function () {
   });
}
module.exports = instance

Bluebird 是一个用于 promises 的 npm 库

在我使用 mocha 的测试文件中,我导入了这个实例文件并通过 var Instance = new instance(options); 创建了对象 我打电话给 Instance.createInstance 但是我收到一条错误消息 TypeError: Cannot read property 'getInstance' of null

问题是 thisBluebird.method 中不可用。创建对 this 的新引用以解决问题。

function instance(options) {
   var that = this;
   this.createInstance = Bluebird.method(function() {
               that.getInstance();
    });

   this.getInstance = Bluebird.method(function () {
   });
}
module.exports = instance