I was wondering how arrow functions can be used to replace bind. It was my understanding the I could use the arrow function to lexically call this
in a function, but the function doesn't even get called anymore.
The strange thing is I don't get any errors, if I use the arrow function it just fails silently.
constructor(socket: SocketIO.Socket
{
// Works
socket.on(this.onLogin.name, this.onLogin.bind(this));
// Doesn't work?
socket.on(this.onLogin.name, (data: LoginDetails) => this.onLogin);
}
public onLogin(loginDetails: LoginDetails) {
console.log(this.onLogin.name + " " + this.socketID);
}
您必须在arrow函数内调用该函数。
socket.on(this.onLogin.name, (data: LoginDetails) => this.onLogin(data));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.