课程: JavaScript Practice: Object-Oriented Programming
Solution: Create a class with get and set - JavaScript教程
课程: JavaScript Practice: Object-Oriented Programming
Solution: Create a class with get and set
- [Instructor] To create the Order class, we'll use the Class keyword. Inside the constructor function, the object takes three parameters: restaurant, total, and customer. There are two additional properties we need to add to the class: foodStatus, which is initialized to zero, and validFoodStatuses, which is an array containing the integers zero through three. Now we can make the getter function for orderStatus. To show the appropriate orderStatus message, I'll use a switch statement that returns a different string for different food status codes. Lastly, I'll create the setter function for orderStatus. It takes one parameter, the new status. I'll first check if the food status is valid by searching the validFoodStatuses array for the new status value. If it's a valid status, update the food status value. Otherwise, set food status to null. When we run the code, we can see all of our tests are passing.