gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 09:33:04 25 4
gpt4 key购买 nike

我有一个如下所示的函数:

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

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

Bluebird 是一个用于 Promise 的 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

关于javascript - 类型错误无法读取属性 Javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31434178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com