Micard Micard - 2 months ago 5x
Javascript Question

Javascript make new object accessible in all scopes

I have a login script on a node.js server, so when a user logs in, I create a new object for that user, for example:


db.query('QUERY', function(err, rows, fields){


var User = new _User(row[0].id, row[0].name, ...);
// Continue on

} else { throw err }


Now how can I make it so that I still create a new _User object every time somebody logs in and make it globally accessible. I want to store all current user information in a class and be able to easily retrieve it at any point of time as well as use all the class methods.

EDIT: I don't know if just initializing
var Player;
somewhere at the top is a good idea since it will be overwritten. Yeah, I need some advice :)

P.S.: login script is within a socket event, and I don't mind to re-write the code to fit the adjustments!

P.P.S: I don't want to store the info inside of the socket.


If a socket represents a connection to a user, you can just add a socket._user property to the socket object itself. If you then want methods on your User object, you can add a prototype to the User constructor.