I have a basic question about class definition in JavaScript. Let me explain my problem, here is my class (this is a simplified version for the sake of clarity):
var RemoteCursor = function(canvas_id) {
this.ws_sendjoingroup = function() {
console.log('Dummy log just for debug');
}
this.ws_cursor_onopen = function() {
console.log('ws_cursor_on open: (debug)');
this.ws_sendjoingroup();
}
this.ws_start_remote_cursor = function() {
this.ws_remote_cursor = new WebSocket('ws://localhost/ws');
this.ws_remote_cursor.onopen = this.ws_cursor_onopen;
}
}
I call this class inside my HTML page like this:
<script>
window.onload = function() {
var cursor1 = new RemoteCursor("usimage_cursor");
cursor1.ws_start_remote_cursor();
}
</script>
But when the onopen callback fires, inside the function ws_cursor_onopen the context is different, this have nothing defined and I got the error:
Uncaught TypeError: this.ws_sendjoingroup is not a function!
The typeof(this.ws_sendjoingroup) is undefined
How can I pass the function inside my instance of RemoteCursor as the callback for the onopen?