First, we're going to get started adding the users class and then. We'll finish it up adding all the methods. For now though, we can start defining the class, I am going to comment out the Person class we just added, since we do want it in place as documentation. But we're not going to exactly use it inside the app. We'll delete it a bit later once we are more comfortable with classes.
Now, we're going to start by creating our users class using the class keyword, class Users. We're then going to go ahead and open up and close our curly braces, and inside here we can specify any methods we like, such as the constructor function. We are going to define a constructor function, although they are completely optional when creating classes. We're going to set up our constructor function by name, then we'll have our arguments list followed by the opening and closing curly braces:
class Users {
constructor () {
}
}
Now the constructor function for users, unlike person, is not going to take any arguments. When we make a new users instance, we just want to start with an empty array of users. We're going to be making this new instance over inside server.js when we first start the app, that is going to happen up top of the code. And down below, we'll actually be using the methods when someone joins a room, leaves a room, or manipulates a room in whatever way they want. That means that all we need to do is set this.users, the users property, equal to an empty array:
class Users {
constructor () {
this.users = [];
}
}
This is going to be the array we've defined at the top of the users.js file. The next thing we're going to do, now that we have our constructor function in place, is create the addUser method. We're going to create that just below the constructor function by defining it like we did for getUserDescription. We're going to set up the arguments list, this one is going to take some arguments we'll specify those in a moment, and we're going to open and close our curly braces for the actual function code:
class Users {
constructor () {
this.users = [];
}
addUser () {
}
}
The three arguments we're going to require is id, name, and room. In order to add a user to the users array, we need those three pieces of information. Once we have them actually adding them to the list is going to be really easy.
I'm going to get started by creating a variable user so we can make an object to push on to the array. In users, we'll set an id property equal to the id argument, and we'll do the same thing for name and finally for room:
addUser (id, name, room) {
var user = {id, name, room};
}
}
Now we have a user object with those three properties we can go ahead and push it on the array, this.users.push, to add an object on to the end, and the thing we're going to be adding on to our array is the user variable:
addUser (id, name, room) {
var user = {id, name, room};
this.users.push(user);
}
Now that we have this in place, we are basically done. The last thing I'm going to do is go ahead and return the successfully created user, return user just like this:
addUser (id, name, room) {
var user = {id, name, room};
this.users.push(user);
return user;
}
And there we go, addUser is complete. We're not going to wire it up just yet but we can add a test case for addUser.