Static Validation Method0:00
JavaScript classes have static members and these work a lot like they do for static members in php. There is a slight little difference as to how we access them from inside of the class that they are defined, but we'll talk about that here in a moment. First of all, we just need to define a static member, and what I want is a method that we can use to validate the range values. So that whenever we create a new game, we want to validate the minRange, the maxRange, and the maxAttempts. So that we would do something like this. We would call initRangeValues, and then we would pass in all of the information that we need to validate this. So in this case, the value would be minRange, but then we also want to check the bounds. Is this value within the bounds of the game?
So in this case, the value would be minRange, but then we also want to check the bounds. Is this value within the bounds of the game? So we would have a lower bounds, which we could say that the lower bounds is zero. You can't have a minRange value that is less than zero. So really we don't even want zero. And the upper bounds in this case for the minRange would be maxRange, because we don't want the minRange and the maxRange to be the same value, and we want minRange to be less than maxRange. So we have our lower bounds of zero, we have our upper bounds of the maxRange. And then when it comes to setting the maxRange, we essentially want to do the same thing,
So we have our lower bounds of zero, we have our upper bounds of the maxRange. And then when it comes to setting the maxRange, we essentially want to do the same thing, except that there is a little bit difference here. The value is going to be maxRange. The lower bounds in this case is going to be minRange, because we don't want a maxRange less than the minRange. So that's what we need there. As far as the upper bounds are concerned, you know, there is no limits. We could go to infinity if we wanted to. So really we don't need to check the upper bounds for setting the maxRange.
Implementing Range Validation1:51
We could go to infinity if we wanted to. So really we don't need to check the upper bounds for setting the max range. So whenever we actually implement this initRangeValues, we need to take that into account. And then there's the max attempts. And we should validate this, but I'm not. So let's implement initRangeValues. So we have this object that's going to have a value. And then it's going to have, well, no. It's going to have a lower bounds and then an upper bounds. And the order in which these are defined really doesn't matter,
It's going to have a lower bounds and then an upper bounds. And the order in which these are defined really doesn't matter, because they are properties on an object that we are extracting. But I like to keep things in order. But the upper bounds, we need a default value of 0, because we won't always need to validate the upper bounds. And then we will have a default value for these options here. Okay, so the first thing we need is our number. So we will pass the value to the number function. That will convert it, or at least it will try to convert it into a number.
So we will pass the value to the number function. That will convert it, or at least it will try to convert it into a number. And we can check if isNaN(num), then we want to essentially fail. We will throw an object that has a message property. And it will say that the value must be numeric. So that's the first check. The second check is for the lower bounds. So if num is less than lowerBounds, then that's a problem. So we will throw another object that has a message property. It says value cannot be less than.
So we will throw another object that has a message property. It says value cannot be less than. And then we will include the lowerBounds value. And then we essentially want to do the opposite for upperBounds. So if num is greater than upperBounds. Remember, we won't always need to check the upperBounds. So we really need to check, first of all, do we have upperBounds to check? And if so, then we will check if num is greater than upperBounds. And then if so, we will throw another object that has a message property. And it will say value cannot be greater than the upperBounds.
And then if so, we will throw another object that has a message property. And it will say value cannot be greater than the upper bounds. So those are the validation checks. So that finally, if we get here, we have a valid number that we can use. And we want to assign that to minRange and maxRange. So of course, we want to call this static method from inside of the class. And we do that very easily. We have to use the name of the class, which of course is Game. And then the name of that static member. So Game::initRangeValues.
Calling Static Methods4:19
And then the name of that static member. So Game::initRangeValues. It doesn't matter if we are inside of the class or outside of the class. We reference our static members the same. We use the class name, dot, and then the static member name. That's very different from php, to where inside of the class we would have self::. Nope, just Game. All right, and now that we have this, we could use that to validate the values for our factory function.
Adding Getters and Setters4:44
All right, and now that we have this, we could use that to validate the values for our factory function. So if we wanted to, we could come in here and we could say minRange equals to Game::initRangeValues(). And then we could supply all of that information that we needed. I'm not going to go through that process, but we could do that here if we wanted to, because this is now a static method that we could use. And now that we have this ability to essentially validate the min and maxRange values, I want to make it possible to go ahead and change the values of these properties. Now, minRange and maxRange are currently private, because that's how we define them using the pound sign.
Now, minRange and maxRange are currently private, because that's how we define them using the pound sign. And I want to leave that the same. I want these as private, but I want to essentially provide an accessor to access those values. And in php, classes don't have accessors. I mean, yes, you can write a method to get and set values for private fields, but in JavaScript, we can define actual accessors, so that it looks like this, to where if we wanted to change the value of EasyGame2's maxAttempts,
so that it looks like this, to where if we wanted to change the value of EasyGame2's maxAttempts, we wouldn't have to call a method called setMaxAttempts. Instead, we would just assign it a new value, and that's it. The accessor gives us that ability. So let's create an accessor. In fact, we're going to create accessors for minRange, maxRange, and maxAttempts. So the first thing we want is a get accessor. This is going to get the value of something. So we start with the get keyword,
This is going to get the value of something. So we start with the get keyword, followed by the name of the property that we want to define. So I want this to be the getter for the minRange value, so we are just going to return this.minRange. That gives us access to the minRange value, and that's it. We can't set anything yet, but we can get that value. But I want to be able to set it too, so we can have a set keyword, followed by the same property name, but this is going to have a parameter,
followed by the same property name, but this is going to have a parameter, which we can call it whatever, but I typically just call it value. And the idea is that these are essentially functions that are going to execute. It executes to return a value. It executes to set a value. So basically what I want to do here is exactly what we did inside of the constructor. We'll say minRange equals, and then we will use our initRange values to supply the appropriate information.
and then we will use our initRange values to supply the appropriate information. The value is, of course, going to be the value that is supplied to it. The lower bounds in this case is going to be zero because this is the minRange. We don't want anything less than zero. But then the upper bounds is going to be the maxRange. So we will say this maxRange, and that is going to allow us to validate this input as well as set a new value for minRange.
and that is going to allow us to validate this input as well as set a new value for minRange. And of course, if it fails, it fails. But then we essentially want to do the same thing for the maxRange properties. So we will create another set of accessors. The get accessor for maxRange will return our private maxRange property. And then the setter for maxRange will set the value for the maxRange private value. So once again, we init the range values. The value that we supply is the value. The lower bounds in this case is going to be the minRange.
The value that we supply is the value. The lower bounds in this case is going to be the minRange. But then we don't have an upper bounds, so we don't need to include that. And then finally, the attempts. And once again, we should validate it, but I'm not going to. We're just going to have the maxAttempts to where we return that property. And then we will set that property by just setting the value there. And one of the really nice things about accessors is that we don't have to use them just in classes. We can also define accessors inside of an object.
Accessors in Object Literals9:02
is that we don't have to use them just in classes. We can also define accessors inside of an object. All we have to do is, well, do the exact same thing that we did. So if we wanted to provide accessors for minRange, maxRange, and maxAttempts, you know what, I should just copy and paste that because that's almost what we need. So let's see right here. There's our getter for minRange, maxRange, and then maxAttempts. I'm just going to copy all of that so that inside of our object, we now have these getters and setters.
I'm just going to copy all of that so that inside of our object, we now have these getters and setters. But this is going to be a little bit different because these are accessing actual values here. So we do need to make some changes so that instead of this dot and then a property, we just need to reference the minRange, the maxRange, and the maxAttempts. So there's a few things we need to change, but it's going to be relatively straightforward. And of course, since this is inside of an object literal,
but it's going to be relatively straightforward. And of course, since this is inside of an object literal, we need to use object literal syntax. So every one of these accessors are separated by commas, and we have just a few more to get rid of the this # sign. And that is going to give us accessors both for our class and for all of our objects that we create using our factory function. So now if we need to set new values for those properties after we create the objects, we can. And we have complete control, so we can validate that input.
after we create the objects, we can. And we have complete control, so we can validate that input and be sure that we are working with appropriate values.
