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
16k31 gold badges74 silver badges89 bronze badges
asked Feb 6, 2015 at 9:06
3
  • Nice job describing your problem; +1. Commented Feb 6, 2015 at 9:10
  • Indeed, very clean description. +1 Commented Feb 6, 2015 at 9:10
  • Isnt the line Client.ConnectedClients_socket[socket].getNetworkId() should be Client.ConnectedClients_socket.socket.getNetworkId() ? Commented Feb 6, 2015 at 9:12

2 Answers 2

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]
answered Feb 6, 2015 at 9:09
Sign up to request clarification or add additional context in comments.

Comments

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.

answered Feb 6, 2015 at 9:09

4 Comments

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?
@Christian.tucker Bracket notation accepts string only.
@Orion—bracket notation "accepts" any expression, but as property names are strings the expression will be evaluated and then converted to a string using the internal ToString method.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.