Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] ws回调函数的this绑定问题 #64

Open
Gk0Wk opened this issue Apr 29, 2022 · 1 comment
Open

[Feature] ws回调函数的this绑定问题 #64

Gk0Wk opened this issue Apr 29, 2022 · 1 comment
Assignees

Comments

@Gk0Wk
Copy link

Gk0Wk commented Apr 29, 2022

版本:2.9.1

复现方式:

import { createWebsocket } from 'qq-guild-bot';
class Test {
    constructor(ws: any) {
        ws.on('READY', this.test);
    }
    test() {
        console.log(this);
    }
}
const test = new Test(createWebsocket(/* 配置文件略 */));
test.test();

输出结果:Ready回调Test.test()输出的this为WebsocketClient,而非test实例。


所以是不是应该改成绑定为对应的类实例会好一些?因为一些时候需要将功能封装,不这样的话会很难受。

@foreversimon
Copy link

你应该传入this.test.bind(this)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants