Javascript 获取对象内部的正确函数
在此代码中,在第15行 此指针指向套接字,而不是我的对象网络。我应该怎么做才能真正获得网络对象?很抱歉,我在谷歌上搜索了一段时间,甚至不知道该键入什么来找到答案Javascript 获取对象内部的正确函数,javascript,Javascript,在此代码中,在第15行 此指针指向套接字,而不是我的对象网络。我应该怎么做才能真正获得网络对象?很抱歉,我在谷歌上搜索了一段时间,甚至不知道该键入什么来找到答案 var Network = function(ip, port){ this.host = "ws://"+ip+":"+port; this.socket = new WebSocket(this.host); this.socket.binaryType = "arraybuffer"; var ByteBuffer
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>");
}
}
}
付诸表决:
并在需要对象时引用self。在网络对象中,缓存对该对象的引用:
然后你可以用self而不是这个来访问它
由于JavaScript闭包的工作方式,您可以从内部访问self,即时调用是您的朋友:
this.socket.onmessage = function(that) {
return function(e) {
// Your callback code here, where `that` is referencing your `Object Network`
}
}(this);
这是一个普遍的问题。一个选项是使用闭包维护对它的引用:
// 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,强制处理程序在网络为this而不是套接字的情况下执行:
在您的例子中,还有一个是不要使用它来引用handleLoginAnswer函数,因为它无论如何都是在同一个地方定义的。如果更改其定义,请执行以下操作:
this.handleLoginAnswer = function() { /* ... */ };
为此:
var handleLoginAnswer = function() { /* ... */ };
以及以下人士的电话:
this.handleLoginAnswer(bytearray);
致:
这个问题的一个常见答案是:
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
}
};
保存对此的引用,然后使用该引用而不是此引用
将其保存到第1行附近的某个局部变量,并在回调中使用该变量。与每个注释一样,将其分配给一个变量。
this.handleLoginAnswer = function() { /* ... */ };
var handleLoginAnswer = function() { /* ... */ };
this.handleLoginAnswer(bytearray);
handleLoginAnswer(bytearray);
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
}
};
var Network = function(ip, port){
var _this = this;
...
case 2: _this.handleLoginAnswer(bytearray); break; //line 15