中的对象成员我在构造函数中设置了一个变量,我无法从其中一个方法访问它,因为“this”引用了该函数(它不应该是这种情况) 。 下面是它看起来likecode:Nodejs无法访问方法
class myMiddleware {
constructor(variable) {
this.variable = variable;
}
middleware(packet, next) {
console.log(this.variable)
}
}
我使用VS 2017年的事项情况。
编辑:我使用这个作为socket.io套接字中间件。 这里是我怎样,我这样做:
const myInstance = new myMiddleware(myVariable);
socket.use(myInstance.middleware);
你如何实例化类并调用方法?适用于我。 –
'instance.test.bind(instance)' – robertklep
@BenFortune编辑了这个问题 – aslad