Submodule 12.3: Constructor functions

  • Introduction to Constructor
  • Constructor syntax
  • Methods inside Constructor Functions
  • Adding Properties and Methods

Introduction to Constructor

In the previous submodule, we learned about JavaScript objects. Although objects give us a lot of capabilities, they have one drawback, they are not easily reusable.

This means that if we want to have more than one objects with the same properties and methods on a page, there is no easy way to achieve it. 

The solution to the above comes with the usage of constructor functions

With constructor functions, we can create as many replicates as we may need, without writing excess code.  

For example, imagine that we want to create 1.000 cycles that are randomly moving ( we will learn how to do this in another submodule).

If we were to do that using JavaScript objects, we would have to write the same code, with different names, 1000 times.

We only need one constructor function to achieve the same result.