JavaScript object reference

So, I'm pretty new to JavaScript, and although I know little tricks to do similar things in Java, the same tricks don't apply here, or at least I can't figure out how to use them, so currently I use two objects as collections:

var ConnectedClients_socket = {};
var ConnectedClients_networkId = {};

Hold a key / value pair to associate networkIDwith the client, and then a duplicated object that uses the socket as the key. Any optimization will be great, I will grab the client with sockets and identifiers throughout the application.

To the problem:

When disconnecting, I need to remove the client instance from the object ConnectedClients_networkId. In my opinion, this is done using the keyword delete.

Here is the code I'm trying to use for this:

console.log('socket disconnected');
delete Client.ConnectedClients_networkId[Client.ConnectedClients_socket[socket].getNetworkId()];
delete Client.ConnectedClients_socket.socket;

:

TypeError: Cannot call method 'getNetworkId' of undefined

,

Client.ConnectedClients_socket[socket]

undefined, Client.ConnectedClients_socket.socket . JavaScript, , .

:

var Client = function(networkId, socket, ...) {
    this.networkId = networkId;
    this.socket = socket;
    ...
    ConnectedClients_socket.socket = this;
    ConnectedClients_networkId.networkId = this;
}
+4
2

.

    delete Client.ConnectedClients_networkId[Client.ConnectedClients_socket.socket.getNetworkId()];

JavaScript, .

+1

?

Client.ConnectedClients_socket['socket']

.

Client.ConnectedClients_socket.socket

.

.

Client.ConnectedClients_socket['someArr[]']

var x = 'socket';
Client.ConnectedClients_socket[x]
+2

All Articles