在此代码中,在第15行上 "这"指针指套接字而不是我的对象网络。我该怎么做才能真正获得Network对象?对不起," noob"问题,但我在谷歌搜索了一段时间,我甚至不知道输入什么来找到答案..
var Network = function(ip, port){
this.host = "ws://"+ip+":"+port;
this.socket = new WebSocket(this.host);
this.socket.binaryType = "arraybuffer";
var ByteBuffer = dcodeIO.ByteBuffer;
this.socket.onopen = function(){
console.log('Connected to the WebSocket server');
};
this.socket.onmessage = function(e){
var bytearray = new Uint8Array(e.data);
switch(bytearray[0])
{
case 2: this.handleLoginAnswer(bytearray); break; //line 15
default:
alert("received a wrong packet")
}
};
this.handleLoginAnswer = function(packet){
var bytearray = new Uint8Array(e.data);
var reader = ByteBuffer.wrap(bytearray);
var opcode = reader.readUint8();
if(opcode != 2)
return;
var result = reader.readUint8();
switch(result){
case 1: displayValidate("Sucessfully registered"); break;
case 2: displayError("Username is already taken"); break;
case 3: displayError("Email is already taken"); break;
}
function displayValidate(message){
$("#register-messagebox").html("<div class=\"alert-message\" style=\"background-color:#27ae60;\">" + message + "</div>");
}
function displayError(error){
$("#register-messagebox").html("<div class=\"alert-message\" style=\"background-color:#e74c3c;\">" + error + "</div>");
}
}
}
答案 0 :(得分:2)
把:
var self = this; // or other name to suit
并在需要对象时参考self
。
答案 1 :(得分:2)
在Network
对象中,缓存对this
的引用:
var self = this;
然后您可以使用self
代替this
case 2: self.handleLoginAnswer(bytearray); break; //line 15
由于JavaScript闭包的工作方式,您可以从
中访问self
答案 2 :(得分:1)
立即调用是你的朋友:
this.socket.onmessage = function(that) {
return function(e) {
// Your callback code here, where `that` is referencing your `Object Network`
}
}(this);
答案 3 :(得分:1)
这是一个常见问题。一个选项是使用闭包来维护对它的引用:
// Get a reference to the network
var network = this;
this.socket.onmessage = function(e){
var bytearray = new Uint8Array(e.data);
switch(bytearray[0]) {
// Access the outside-defined 'network' here
case 2: network.handleLoginAnswer(bytearray); break;
default:
alert("received a wrong packet");
}
};
另一个是使用Function.bind
来强制执行处理程序,network
是this
,而不是socket
:
this.socket.onmessage = function(e){
var bytearray = new Uint8Array(e.data);
switch(bytearray[0]) {
// It's safe to use 'this' here
case 2: this.handleLoginAnswer(bytearray); break;
default:
alert("received a wrong packet");
}
// Bind the function, so that it is always executed in this context
}.bind(this);
在您的情况下,另一个是不使用this
来引用handleLoginAnswer
函数,因为它无论如何都定义在同一个地方。如果你改变它的定义:
this.handleLoginAnswer = function() { /* ... */ };
到此:
var handleLoginAnswer = function() { /* ... */ };
来自:
的电话this.handleLoginAnswer(bytearray);
要:
handleLoginAnswer(bytearray);
答案 4 :(得分:1)
这个问题的一个常见答案是:
var myclass = function () {
var self = this;
self.host = 'my host';
self.goHere = function () {
//here use self to refer to myclass or this to refer to goHere function
}
};
答案 5 :(得分:1)
保存对此的引用,然后使用引用而不是this
var Network = function(ip, port){
var _this = this;
...
case 2: _this.handleLoginAnswer(bytearray); break; //line 15