- Create an object
- Create methods and properties for that object
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:
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.
Removes the duplication of methods that was found in functional instantiation which improves memory management.
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 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.
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.
To use this method, you have to create an object, decorate it and then return it from the constructor function.
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.
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.
It is a little more complex in its design when compared to the other three methods.
The size of your code and performance requirements should determine which instantiation method that you use.
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:
First Impressions Count — Why Doesn’t Your Github Repo Have a ReadMe File?
Here are 5 Layouts That You Can Make With FlexBox