Hello, Explorer! Today, we're unveiling a core concept of Object-Oriented Programming (OOP): JavaScript classes. These classes act as blueprints, allowing us to generate objects — instances of these blueprints — each carrying unique properties and behaviors. This lesson has one aim: to understand what classes are, how to create them, and their purpose within your JavaScript code.
Think of a JavaScript class as a construction blueprint. By following the blueprint, we can create specifically structured objects, each filled with different values.
Here, Fruit is our blueprint or class. This blueprint then enables us to generate a variety of fruit objects with specialized attributes, much like constructing a building.
Creating an instance of a class, essentially bringing an object to life from this blueprint, uses the new keyword:
In this example, apple is a specific instance or object of our Fruit class, much like a single building constructed from a shared blueprint.
To bestow behavior upon our class, we incorporate methods — these are functions that belong to a class. Let's add a straightforward method inside our class:
