0

I'm creating a chat application in Node.js using MVC design. I'm confused about whether certain methods should belong to the 'Room' object or the 'User' object. I need these methods:

checkIfUserExists(users_array)
checkIfRoomExists(rooms_array)

If this was C++, I may have created static functions to do this in their respective classes, but I'm not sure what the best practice in Javascript is. Any suggestions?

vjdhama
  • 4,878
  • 5
  • 33
  • 47
  • 2
    If you have a Room on which to call a method, then clearly it exists. It would seem that you'd want the `checkIfRoomExists` to be on whatever it is that holds a set of Room objects. But then you've provided very little as far as a concrete example of the problem, so who knows... – Lye Fish Apr 04 '15 at 18:05
  • You might want to give an example as to how your arrays should look like. I'll take a shot in the dark here, and say that these functions should not exist to begin with (unless during debugging, when you simply want to make sure that your code does not, for ex. accidentally put non-user objects into an array of user objects). – Domi Apr 04 '15 at 18:08
  • My Room object holds a Users array that contains all the users in the room. checkifUserExists() function is to check server-side whether a user exists already(duplicate) before adding him to the array. Similarly I'll need to check whether a Room exists before allowing the User to create the room. – Helluva Guy Apr 04 '15 at 18:12
  • I basically have two models: User(just contains data like name and id) and Group(contains array of all current users). – Helluva Guy Apr 04 '15 at 18:16
  • what about Room.hasUser(userID) and User.isInRoom(roomID) hm ? – webduvet Apr 04 '15 at 19:14
  • This isn't a JavaScript problem.. It is an OOP problem – Ryan Apr 04 '15 at 19:57
  • 1
    Although JS is a prototypal language, [you can define a static method on a JS class](http://stackoverflow.com/q/7694501/1202461). – Leonid Beschastny Apr 04 '15 at 20:03
  • I mean, if you need static methods - use them, especially if this will make your code easier to write / read / maintain / debug. – Leonid Beschastny Apr 04 '15 at 20:05

0 Answers0