Classes and Constructor Functions

12/06/2015

Creating batches of objects

As you leave the world of object oriented programming with Ruby, and enter the world of functional programming with Javascript you may be wondering how do I create multiple instances of a object? The answer lies in the constructor function. Like classes constructor functions act as blueprint which endows each instance of a object with predefined behaviors and states. To create a constructor function you declare it using the function keyword, followed by the capitalized name of the function. You can then pass any properties that your objects will have via parameters. Inside of the function you assign each parameter to the offical property name prepended with the this keyword. Finally, you can add the methods each of your objects will have.

How to Create New Instances

Now that you have your blueprint, it's time to create a new object with it. In order to create this object you declare a variable name, and assign it to new keyword along with the name of the constructor function with any needed argruments. Behind the scenes the new keyword does the heavy lifting of creating a empty object. After the empty object is created the constructor function is called. Any arguments are then assigned as values to the properties defined in the function body, and methods are then added to the object. Finally, the object is returned and assigned to the variable name.

Differences Between Ruby Classes and Constructor Functions

So far constructor functions have parrelled classes pretty well. However, there are some differences between the two. One of them being that in Ruby you have to use the special class keyword to create a new class. However, there isn't a special keyword used in the creation of a constructor function. While you will often see constructor functions with the name capitialized that is a style convention which makes it easier to identify them in code. In actuality you could write a constructor function using standard function declaration or expression syntax.