Red*_*ood 1 javascript typescript
我想知道如何使用箭头函数来替换bind.我的理解是我可以使用箭头函数来词法调用this函数,但函数甚至不再被调用.
奇怪的是我没有得到任何错误,如果我使用箭头功能它只是默默地失败.
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);
}
Run Code Online (Sandbox Code Playgroud)
您必须在箭头功能内调用该函数.
socket.on(this.onLogin.name, (data: LoginDetails) => this.onLogin(data));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
388 次 |
| 最近记录: |