Photo by Helloquence on Unsplash

Instantiation Patterns in JavaScript

Instantiation patterns are ways to create something in JavaScript. JavaScript provides four different methods to create objects. Regardless of which method you use, every method will provide the following functionality:

  • Create an object
  • Create methods and properties for that object

There are four instantiation patterns in JavaScript. They are:

  1. Functional
  2. Functional-shared
  3. Prototypal
  4. Pseudoclassical

Functional Instantiation

With functional instantiation, we first create a function. Inside the function we create an empty object and add properties and methods to it. We then return this object.

Every time the function is called we will have access to the methods that were created. Here is an example of functional instantiation:

Pros:
For most people that are learning JavaScript this is the method that they learned for creating a new object. For anybody reading your code, it is easy to understand because all the functions are contained within the object. The properties are private since they are contained within the closure scope.

Cons:
Since all the methods are contained within the function, if you create a second instance of that object, you will have duplicated all the properties and methods in memory. If you create a new object using this method, then change any of the methods and create a new instance, the two objects will be referencing different methods.

Functional Shared Instantiation

One of the downsides of functional instantiation is that you duplicate methods in memory every time you create a new object. Functional shared instantiation attempts to overcome that limitation by making the methods shared among all objects.

Just like functional instantiation, we start with a function with an empty object inside and define properties within the function. Methods are defined in another object. We then extend our object with these methods. In the end, we return the object. Every object created by functional shared instantiation will have a pointer to the same methods without duplication. Here is an example of functional shared instantiation.

Pros:
Removes the duplication of methods that was found in functional instantiation which improves memory management.

Cons:
The pointers to the shared methods are created when the object is instantiated. If you modify the methods and then create new objects, they original object and the new object will refer to different methods.

Prototypal Instantiation

Prototypal instantiation utilizes the prototype chain to create objects. Methods are attached to the object’s prototype using the Object.create method.

To start you will create all the methods on a separate object. Then you create a function. Inside the function you use the Object.create method to attach the methods. You will also define any properties inside the function. Then you return the object. Here is an example of prototypal instantiation.

Pros:
Methods are attached to the object’s prototype instead of being returned within the object. Every method is available to every object created without duplicating methods in memory.

Cons:
To use this method, you have to create an object, decorate it and then return it from the constructor function.

Pseudoclassical Instantiation

Pseudoclassical instantiation attempts to overcome the amount of typing required to create an object using prototypal instantiation. Like prototypal, Pseudoclassical instantiation uses the prototype chain.

JavaScript provides most of the functionality that create with prototypal instantiation with the use of the keyword new. Pseudoclassical instantiation utilizes this when creating a new object.

Instead of creating a new variable and assigning Object.create() to it, Pseudoclassical instantiation assigns it to “this”.
To start you create a new function and create properties using the “this” keyword. Methods are assigned to the prototype. To create a new object, you use the keyword “new”. Here is an example of Pseudoclassical instantiation.

Pros:
By utilizing functionality built into JavaScript, Pseudoclassical instantiation is the most optimized method of object creation.

Cons:
It is a little more complex in its design when compared to the other three methods.

Summary

JavaScript developers should be aware of the different methods available to them for creating objects. Each method has its own pros and cons.

The size of your code and performance requirements should determine which instantiation method that you use.

More Articles

Thanks for reading my article. If you like it, please click on clap icon below so that others will find the article. Here are some more of my articles that you might be interested in:
Breadth First Search in JavaScript
First Impressions Count — Why Doesn’t Your Github Repo Have a ReadMe File?
Here are 5 Layouts That You Can Make With FlexBox

Show your support

Clapping shows how much you appreciated Jennifer Bland’s story.