pom.xml
运行代码时出现错误:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
我不确定如何将module.exports = Client;
function Client (info) {
this.concurrency = info.concurrency;
this.queue = async.queue(data, this.concurrency);
this.logger = info.logger;
this.queue.saturated = function() {
this.logger.info('The queue has been saturated');
}
this.messagesQueue.unsaturated = function() {
this.logger.info('The queue is unsaturated');
}
};
传递给饱和和不饱和方法?
答案 0 :(得分:1)
this
或saturated
中的unsaturated
指向函数本身。
所以你可以做三件事,
1)。在您的客户端类中,设置一个指向this
的变量并使用它。
function Client (info) {
const that = this;
this.queue.saturated = function() {
that.logger.info('The queue has been saturated');
}
};
2):绑定this
:您可以将函数内的this
绑定到this
上下文的Client
。
function Client (info) {
this.queue.saturated = (function() {
this.logger.info('The queue has been saturated');
}).bind(this);
};
3):使用箭头功能:箭头功能不绑定this
。使用箭头函数声明时,将其读取为this
或saturated
函数中的 IS NO unsaturated
变量。因此,当您从函数内部访问this
时,它就像简单的闭包变量一样,将Client
用作this
。
function Client (info) {
this.queue.saturated = () => {
this.logger.info('The queue has been saturated');
}
};