0

我有以下 JS 类。我基本上将 Websocket 对象包装在一个类中

    class MyClass {
        
        // Main constructor
        constructor() {
            this.ws = null;
            this.ConnectionState = false;
        }   

        // Heart beat function
        #heartbeat() {
            // Do some stuff
        }

        // Initialization of the websocket
        Init() {                                    
            this.ws = new WebSocket("some url..."   );

            this.ws.onopen = function (event) {         
                this.ConnectionState = true;            
                this.#heartbeat();  // << The error is raised here
            }       
        }
    }

运行方法Init()时,出现错误: Uncaught TypeError: Object must be an instance of class MyClass at WebSocket.Init.ws.onopen

这发生在行中: this.#heartbeat();

有人可以帮忙吗?

谢谢

4

0 回答 0