js中成员函数如何调用自身
function SocketIO() {
this.socket = null;
this.reconnect=null
}
SocketIO.prototype.start = (server)=> {
let self = this;
this.socket = so(server);
this.socket.on('connect', ()=> {
self.waitType()
});
this.socket.on('disconnect', ()=> {
self.socket = self.start(self.reconnect);
});
};
SocketIO.prototype.waitType = ()=> {
let self = this;
process.stdin.setEncoding('utf8');
process.stdin.on('readable', () => {
let chunk = process.stdin.read();
let cmds = chunk.trim().split(' ');
switch (cmds[0]) {
case 'connect':
self.reconnect = cmds[1];
break;
}
});
};
2 回复
这样输出的this会指向全局对象的 给匿名函数取个名字就好 ··· SocketIO.prototype.waitType = function waitType(a, b){ waitType(a, b, c) } ···
@gjc9620 感谢啊