Classes
You may wonder, if classes do the exact same thing as simply defining an object, why do we even need classes? The answer is that classes are essentially blueprints for object creation. This means that we need to do much less typing if we need to create 20 dogs when we have a dog
class. If we have to create the objects, we will have to specify all the properties' names each time. And it would be easy to make a typo and misspell a property name. Classes come in handy in these sorts of situations.
As shown in the previous section, we use the class
keyword to tell JavaScript we want to create a class. Next, we give the class a name. It is the convention to start class names with a capital letter.
Let's have a look at all the different elements of a class.
Constructors
The constructor
method is a special method that we use to initialize objects with our class blueprint. There can only be one constructor in a class. This constructor contains properties...