4

So, I'm pretty new to JavaScript, and while I know the little tricks to do things like this in Java the same tricks don't really apply here, or at least I can't really figure out how to use them, so I'm currently using two objects in the form of collections:

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

The hold a key/value pair to link a networkID to a client and then a duplicated object that uses the socket as a key. Any optimizations would be great, I'll be grabbing the client by the socket and ID a lot throughout the application.

On to the problem:

Upon disconnection I need to remove the client instance from the ConnectedClients_networkId object. From my understanding this is done using the delete keyword.

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

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

However this is giving me the following error:

TypeError: Cannot call method 'getNetworkId' of undefined

which shows that

Client.ConnectedClients_socket[socket]

is returning undefined, while Client.ConnectedClients_socket.socket is being removed successfully a line later. I've looked up a few different pages on getting a value from a key in JavaScript, and I don't see what I'm doing wrong.

Here's the constructor for a client:

var Client = function(networkId, socket, ...) {
    this.networkId = networkId;
    this.socket = socket;
    ...
    ConnectedClients_socket.socket = this;
    ConnectedClients_networkId.networkId = this;
}
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Hobbyist
  • 15,888
  • 9
  • 46
  • 98

2 Answers2

2

Quotes?

Client.ConnectedClients_socket['socket']

However, there is no need for this notation here.

Client.ConnectedClients_socket.socket

Is fine.

Bracket notation can be used for accessing members with special characters or by variable value.

Client.ConnectedClients_socket['someArr[]']

or

var x = 'socket';
Client.ConnectedClients_socket[x]
Bitwise Creative
  • 4,035
  • 5
  • 27
  • 35
1

You need to use dot notation there.

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

Bracket notation will parse it as JavaScript, so it's useful for dynamically getting a property.

Scimonster
  • 32,893
  • 9
  • 77
  • 89
  • 1
    I'm not exactly sure I understand how this works. I've read in numerous areas that the bracket and dot notation were completely interchangeable. Could you please link me somewhere that this is explained in detail? – Hobbyist Feb 06 '15 at 09:11
  • 1
    http://stackoverflow.com/questions/1168807/how-can-i-add-a-key-value-pair-to-a-javascript-object-literal/1168814#1168814 – Scimonster Feb 06 '15 at 09:12
  • @Christian.tucker Bracket notation accepts string only. – Lewis Feb 06 '15 at 09:23
  • 3
    @Orion—bracket notation "accepts" any [*expression*](http://ecma-international.org/ecma-262/5.1/#sec-11.2.1), but as property names are strings the expression will be evaluated and then converted to a string using the internal *ToString* method. – RobG Feb 06 '15 at 09:31