In this third part of exercise 10, we're going to take a look at the class definition as a way to solve the problem. Now, as with any approach in JavaScript, there are multiple ways to solve this. But I want to use the class definition so that that end result is the same as we got in the previous two parts, meaning that the prototype has the default values on it, and the prototype has the damage per second method on it. Some of the ways that could be used to solve this with a class definition such as extend keyword don't produce those exact same results. Even though in the end we would get the same number. When we call the damage per second, the object it creates is a bit different.
So let me jump to sublime. And I'm going to comment out what we did pre Obviously. Now first thing, let me scroll this up a bit so that we can see better. First thing I want to do is I'm going to define the class definition for game character. And use same terms as we use curly braces. Everything inside the curly braces defines that class.
I'm going to set up the constructor. And we're going to allow speed to be passed in strength to be passed in and hit points to be passed in. Now I'm going to use the conditionals to determine whether one of those values was passed in so if speed very similar to what we've done, this dot speed equals speed. Same thing with strength if strength This strength equals strength. And then if you hit points, this dot hit points equals hit points. Okay, so that will define everything for us.
Now by adding the method down here damage per sec, this will get placed on the prototype. In everything inside the constructor will get placed on the actual object, not in the prototype. So we've got our class defined, but how do we set defaults on the prototype? Well, one way to do this remember, nothing has changed about JavaScript. We're still using objects and we still can reference the prototype. So one way to do this is simply game character dot prototype dot Speed equals six game character dot prototype dot strength equals eight.
So very similar to how we did in the previous movie, game character dot prototype dot hit points equals 150. All right, now we can create our sample character. set that equal to new game character and we're going to pass in a nine, a no and 205. And then we'll console whoops console log damage per second off of character. And we'll see what that is equal to. So I saved that Let's open the page.
Open the console. And yes, indeed we have a 72. Now I just want to display the object really quick. So as you can see an object itself, we have hit points and speed. And it's not till we look at the prototype that we have the other defaults and the function. Okay, so very similar results to what we achieved in the other two exercises.
All right, let's move on to the next section.