Available books
(electronic beeping) - Unlike classical programming languages like Java or C++, JavaScript uses objects within a prototypal inheritance model. In ES2015 or ES6, the class keyword was introduced, but the class keyword is solely syntactical sugar, and does not change JavaScript's prototypal inheritance model. But we can use class declarations as a replacement for functions. In this challenge, you'll be developing an inventory application for a bookstore. You need to create a book class, which provides information about different books in the store. Each book will have a title, author, ISBN, and keep track of the number of available copies. You'll need a way to get each book's availability. If there aren't any copies of the book left, the function should return "out of stock." If there are less than 10 copies, the function should return "low stock." Otherwise, the function should return "in stock." You'll also need a function for selling a book. This will take the number of copies sold, and subtract it from the total number of copies. If no argument is passed, we can default the number of copies to sell to one. Lastly, you'll want a restock function, which takes in a number of copies to restock, and adds it to the total number of copies. If no argument is passed, we can default the restock number to five. You should use JavaScript's class keyword as well as a getter function for the availability method. Pause the video here, develop your solution, and when you're ready, come back, and I'll walk you through my solution. (cheery chiptune music) (electronic beeping) Anything you can write as a class, you can write as a function. So let's start by solving this problem with a function, and we'll refactor it into a class. Our book function will accept four arguments, and set them on the instance of the book. (keyboard clacking) Next, we can declare the getAvailability function on the prototype. If numCopies is zero, we'll return the string "out of stock." If numCopies is less than 10, we'll return the string "low stock." Otherwise, we'll return the string "in stock." (keyboard clacking) We declare this function on the prototype because we don't need to create a new instance of this function each time we make a new object. We can declare it on the prototype, and each book instance can use that prototype's function. We can follow the same structure to declare the sell and restock functions. The sell function takes an argument, numCopiesSold, and subtracts it from the number of available copies. The restock function takes an argument, numCopiesStocked, and adds it to the number of available copies. We can assign a default value to both of these function arguments in the event that the argument isn't passed. (keyboard clacking) Now we can create a new book, and test our restock and sell functions. (keyboard clacking) Let's run this in our console. We can navigate to the start folder with cd start, and then run node, followed by our file name. In this case it's 01_01_available_books.js. You can start typing the file name, and hit the tab key to auto complete. After we run the file, we can see in the terminal we've got "low stock," after restocking we have "in stock," and after selling we have "out of stock." (keyboard clacking) Let's transition this function into a class component. I'll comment out our function component so we can reference it as we create our class component. (scroll wheel whirring) Inside our class, we first define a constructor. We can think of the constructor as a blueprint for our book. Similar to our function, the constructor takes four arguments. (keyboard clacking) Next, let's create our getAvailability method. We can do this directly within the class. (keyboard clacking) You'll notice that all of our book-related code now lives inside of the book class. This is called encapsulation. Now we can use a getter function to retrieve the book's availability. This will allow us to write book.availability instead of book.getAvailability. (keyboard clacking) Lastly, we can add our sell and restock functions directly within the class as well. (keyboard clacking) (cheerful beeping) JavaScript's class keyword is becoming more and more prevalent in the world of web development, so understanding how it works and why you would use it over function notation is going to be vital to your career as a JavaScript developer.
内容
-
-
-
Available books6 分钟 11 秒
-
(已锁定)
Movie object5 分钟 29 秒
-
(已锁定)
Vegetarian dinner3 分钟 11 秒
-
(已锁定)
Technical books2 分钟 33 秒
-
(已锁定)
Private object2 分钟 13 秒
-
(已锁定)
Ticking time2 分钟 17 秒
-
(已锁定)
Classroom attendance3 分钟 14 秒
-
(已锁定)
Dynamic colors4 分钟 15 秒
-
(已锁定)
Coffee calculations3 分钟 21 秒
-
(已锁定)
Food truck3 分钟 4 秒
-
(已锁定)
Train generator3 分钟 31 秒
-
(已锁定)
String permutation3 分钟 11 秒
-
(已锁定)
Five friends3 分钟 43 秒
-
(已锁定)
URLify3 分钟 55 秒
-
(已锁定)
Password strength4 分钟 58 秒
-