What is Pseudo Classical Instantiation?
WHAT IS INSTANTIATION
Currently, there are four instantiation patterns amongst programmers, being
- ES6 Pseudo classical
As of this publication, Pseudo classical is the most optimized pattern and relies on the knowledge of the keywords new.and this.
HOW DOES IT WORK?
Pseudo classical Instantiation works by utilizing two keywords, this and new instead of Object.create(). This(referenced as this henceforth) keyword works by referring to an object. This is decided during runtime and should refer to the constructor function during instantiation. By using this, you don’t need a return statement in your constructor! The keyword new lets “developers create an instance of a user-defined object type or one of the built-in object types that have a constructor function”(MDN).To pass the methods of a constructor to its subclasses, a programmer must use a prototypal chain that allows for access to the methods To see an example along with an explanation, we will use a Human that makes subclasses of the development of a human.
Human is our constructor function, which acts as a guide for any new objects made from it. In the example, it passes the properties of this as the parameters to be set. This shortens the amount of code being written every time you want to make a subclass.
After the constructor is made, it is time to start with the methods.
The prototype property allows for the passing of methods and can be shared with multiple subclasses that can return different values. Methods are made with the constructor name followed by .prototype. and the name of the method
The method in the example takes no parameters and returns the value that this.words equals. A method is called by the name of the subclass and the method name.
Lastly, it comes time for the subclasses, which for an analogy are like people, we are all humans but look different from one another! The subclass is made by the declaration of the subclass followed by the new keyword which
- Makes an empty Object
- Gives access to the prototype
- Binds the properties of the constructor to the new objects
- Returns the Object
Bypassing in the values for the properties in the parameters, programmers can save time and memory by shortening the amount of code that would have to be repeated.
In conclusion, the way Pseudo classical instantiation is invoked allows for a great opportunity to program in an optimized and highly effective manner!
Credits: MDN — Objects