You can play the game. This discard pile itself probably has some rules and maybe even a maximum card number. You'll see we also have a function there for card drag over which we'll see later on. There's a deck and decks have a list of suits and values. For a deck, you would want to shuffle and stack it. Decks have cards and there are single suits and values. And for our card, we're going to want to flip, drag and drop it.
You could imagine if we were building these objects with code, we may want to reuse them. And that's where prototypes come in. Prototype is something available to all objects. And just like any prototype, it's a way of making a plan for an object so that we can reproduce it. Here are some regular objects. In the first one, I have a variable which is equal to an object and that object has a first name, a last name and age and eye color. Next to that is another object and it's exactly the same thing as the first instance.
They're equivalent snippets of code but instead of just using two curly braces, the one on the right uses new object to create an instance of the object. But those variables are only single instances. To create a reusable object, we would need a constructor function which is more of a plan for the person rather than the instance of the person. It's just a function, but once created we can use our plan as many times as we want to create as many people as we need. Let's build our game out a little bit more and look at three examples.
We might have a flashcard game, a game of Solitaire and maybe a game of Hearts. For right now, all we need to know about them is that Hearts is played with a standard deck and you win by having the lowest score. Solitaire is played with a standard deck and you win by going through the deck and following the rules. Flashcards really don't have rules and really you're just playing to practice skills, not to win. How would you group these games? I might start by grouping them into score and no score.
But what about the standard cards verses custom cards like there are with flashcards? For me, I'd address that in the deck section and do a flashcard deck and a standard deck.
- Working with prototype and dynamic elements
- Using bind, call, and apply
- Using the Fisher–Yates formula
- When to use e.preventDefault() and when to avoid it
- Drag-and-drop life cycle
- Using prototypical inheritance to optimize your code