简体   繁体   English

另一种方法是Javascript调用方法

[英]Javascript call method in another method

Now I'm trying to implement Unity Webgl with jslib. 现在我正在尝试用jslib实现Unity Webgl。 I'm so confused about how to call method in another method's function. 我对如何在另一个方法的函数中调用方法感到困惑。 I want to call method Recv when message was coming (ws.onmessage). 我想在消息来临时调用方法Recv(ws.onmessage)。 But, it show "TypeError: this.Recv is undefined". 但是,它显示“TypeError:this.Recv is undefined”。 Could you please help me figure out this source? 你能帮我弄清楚这个来源吗?
Thank you !!!!! 谢谢 !!!!!

Here's my source code 这是我的源代码

var ws = null;
var init_url = "";
var received_msg = "";
var error_msg = "";

var WebsocketLib = {
Hello: function(){
    window.alert("Hello,world!");
},
InitSocket: function(url){
    init_url = Pointer_stringify(url);
    console.log("InitWebSocket: "+init_url);
    ws = new WebSocket(init_url);
    ws.onopen = function(evt){ 
            console.log("Connect");
            isConnected = false;
            ws.send("hello");
        }; 
    ws.onclose = function(evt) { 
            console.log("Close");
            isConnected = false;
        }; 
    ws.onmessage = function(evt) {
            received_msg = evt.data;
            console.log("[recv] "+received_msg);
            this.Recv.call(this);
        }; 
    ws.onerror = function(evt) {
            error_msg = evt.data;
            console.log("[error] "+error_msg);
            this.Error.call(this);
        };
},
Recv: function(){
    console.log("[recv] "+received_msg);
    var buffer = _malloc(received_msg.length + 1);
    writeStringToMemory(returnStr, buffer);
    return buffer;
},
Error: function(){
    console.log("[error] "+error_msg);
    var buffer = _malloc(error_msg.length + 1);
    writeStringToMemory(error_msg, buffer);
    return buffer;
}
}

Inside of ws.onmessage this will refer to ws (as we're inside a method of ws) and not WebsocketLib . 在ws.onmessage中, this将引用ws (因为我们在ws的方法中)而不是WebsocketLib

However, inside Initsocket, where you define the handlers, this would correctly ( in the sense that this is what you want ) refer to the WebsocketLib object, so you can create a bound function to bind the outer this value to be used as this inside the event handler, like this: 但是,在你定义处理程序的Initsocket里面, this会正确地( 在某种意义上这就是你想要的 )引用WebsocketLib对象,所以你可以创建一个绑定函数来绑定外部的 this值来用作this内部事件处理程序,如下所示:

ws.onmessage = function(evt) {
        received_msg = evt.data;
        console.log("[recv] "+received_msg);
        this.Recv.call(this);
}.bind(this); 

in JavaScript the value of this behaves differently than in other languages. 在JavaScript中, this行为与其他语言的行为不同。 Its value depends on how the function is called. 它的值取决于函数的调用方式。 You can read more about it in the Mozilla MDN page . 您可以在Mozilla MDN页面中阅读有关它的更多信息。

To solve your specific problem you can: 要解决您的具体问题,您可以:

InitSocket: function(url){
    var that = this;                                  // [1]
    init_url = Pointer_stringify(url);
    console.log("InitWebSocket: "+init_url);
    ws = new WebSocket(init_url);
    ws.onopen = function(evt){ 
            console.log("Connect");
            isConnected = false;
            ws.send("hello");
        }; 
    ws.onclose = function(evt) { 
            console.log("Close");
            isConnected = false;
        }; 
    ws.onmessage = function(evt) {
            received_msg = evt.data;
            console.log("[recv] "+received_msg);
            that.Recv.call(that);                     // [2]
        }; 
    ws.onerror = function(evt) {
            error_msg = evt.data;
            console.log("[error] "+error_msg);
            that.Error.call(that);                    // [2]
        };
},

In line 1 I bind the this variable to a custom variable that I decided to call that (but you can call it as you want). 在第1行中,我this变量绑定到我决定调用that的自定义变量(但您可以根据需要调用它)。 Then in line 2 I used that instead of this . 然后在第2行,我用that而不是this

Inside the ws.onmessage function the value of this is not referring to the instance of WebsocketLib , so you need to use this "trick" and access the right this value using the one saved in the closure, inside the value of that . 里面的ws.onmessage函数的值this是不是指的实例WebsocketLib ,所以你需要使用这个“绝招”,并访问正确this使用保存在封闭的一个价值,里面的值that

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM