JavaScript Classes

Lesson Objectives

  1. Explain why we need classes

  2. Create a class to define the blueprint for creating objects

  3. Add methods to a class

  4. Set properties on an instance of a class

  5. Make an instance of each class customizable

  6. Create methods to alter the properties of an instance

  7. Make a class inherit attributes from a "parent class"

  8. Create static properties for a class

  9. Create a factory

Explain why we need classes

Sometimes we need to repetitively create new objects with the same attributes. Imagine we wanted to create multiple people. All people have the same basic attributes, so it would be great if we could create a blueprint for our person creation process.

Create a class to define the blueprint for creating objects

When creating a class, it's custom to capitalize the first letter of the variable, so we know it's a class. This follows customs in other programming languages.

Now we can "instantiate" or create new objects using this class. We do this by adding the new keyword before calling the class name like a function.

Add methods to a class

Right now, our object doesn't do anything. Let's have it do some stuff;

These methods can of course take parameters:

If we have multiple methods, don't put commas between them:

Set properties on an instance of a class

If we log the instances of our class, we'll see they don't have any properties:

Let's add some properties with a constructor function. This is a function that gets called once, each time an object is created:

Make an instance of each class customizable

Of course, our constructor function can take params which we can use to alter the properties of the object instantiated. This allows us to customize each instance:

Create methods to alter the properties of an instance

We can of course, alter the properties of an instance, after it is created:

But it's a nice practice to define a method that will alter that:

  • This way, everything is done with methods

  • Other developers can quickly scan the class definition to determine what you'd like them to be able to do with the class

Make a class inherit attributes from a "parent class"

Sometimes we want to have a "parent" class that will have some basic attributes that will be inherited by "child" classes.

We can now add additional functionality:

And we can override previous functionality:

We can even reference the parent class' method and extend its original functionality:

This is most useful on the constructor:

Create static properties for a class

Sometimes you want to define properties that pertain to the class as a whole, not the instance. This allows us to limit, somewhat, what the user of class can do.

Create a factory

  • Sometimes we need to have a factory object that will generate other objects

  • The purpose of the factory is so it can control the creation process in some way

  • This is usually a single object that exists throughout the program that performs a set of functions

    • also called a singleton

You could also have a factory which is an instantiation of a class

Last updated