Instantiation Patterns in JavaScript

Taylor Shephard
3 min readMay 14, 2018

An instantiation pattern in JavaScript is a way to create an object using functions. There are five instantiation patterns: Functional, Functional-shared, Prototypical, Pseudoclassical, and ES6.

Functional Instantiation Pattern

The Functional Instantiation Pattern is the most basic pattern to implement. Here is an example of how to create a functional pattern:

Here we see that an object is created within the function. Afterwards keys and methods can be added to that object. All of this is done within the scope of that function and the object is returned at the end.

Functional-Shared Instantiation Pattern

The Functional-shared Instantiation Pattern is a better method than the functional pattern. It is better because functional duplicates methods in memory every time a new object is created which isn’t preferred, and functional shared shares the methods between all of the objects which allows for the methods to be put into memory only once. Here is an example of how to create a functional-shared pattern:

Here we see that instead of making the methods within the function, the methods are in their own object that is added to the function object by using the method extend. So the methods will now be shared between all objects that are created by using the methods object.

Prototypal Instantiation Pattern

The Prototypal Instantiation pattern uses Object.create() to create a new object. This will allow it to use a prototypal chain. Here is an example of how to create a prototypal pattern:

Here we see that the object was created using Object.create and the object methods were passed in. This way the methods are attached to the object’s prototype .

Pseudoclassical Instantiation Pattern

The Pseudoclassical Instantiation pattern uses the prototypal chain like the prototypal pattern does. Instead of using Object.create() to make a new object the pseudoclassical pattern uses the keyword ‘new’ along with the keyword ‘this’ to create a new object.Here we see how an object is created using a pseudoclassical pattern:

Here we see that the only things in the function’s scope is the keys for the object. The keys are getting added to the object with the keyword ‘this’. So when we make a new instance of the object the keyword ‘new’ is used to make that object. When creating the methods, the word prototype is used because the methods are getting chained to the object’s prototype.

ES6 Instantiation Pattern

The ES6 Instantiation pattern uses the keyword ‘class’ instead of making the object using a regular function. It then becomes a constructor function when the constructor function is made inside of it. The methods for the object is also implemented within the class scope. Here we see how an object is created using an ES6 pattern.

Here we see that ES6 is very similar to pseudoclassical instantiation except for the fact that ES6 uses classes instead of using the word function and has a constructor function which changes it into a constructor. Also it doesn’t use the word prototype in its implementation.

Conclusion

We have now covered all of the instantiation patterns in JavaScript.

--

--