MODERN JAVASCRIPT: Constructor Functions or Classes

Constructor Functions: A constructor is a special type of subroutine called to create an object it prepares the new object for use, often accepting arguments that the constructor uses to set required member variables, if I decided to go deeper into the meaning am afraid I would deviate from what exactly this article is meant for (the constructor or classes). The constructor function is ES5 it way of defining object and adding method with new functions.

Classes: Is template for creating objects. They encapsulate data with code to work on that data. Classes in JS are built on prototypes unlike the traditional method of adding new functions to a constructor in ES5; they have some syntax and semantics that are not shared with ES5. Classes are also known as syntactic sugar it made inheritance easy and other sync ideas of programming. It was easier to initialized object and structure data. It used a keyword Class to start before instantiating an object and it has a default constructor method in it.

Over the few weeks I had struggle on what ideal way to learn about constructor I initially started using constructor functions before I started using classes, I had to admit the difference is very clear and even if some argue they are the same I decided to stop using constructor functions and started learning classes and spend some time using it. ES6 is new and without further ado anything new is for the best, hence I strongly advice myself that since it a new method it gotta be better than using the former ES5 method. Off the record I advice myself to find out more and not to seemly assume and never understand why.

A week ago I was honored by the presence of some intellectuals in javaScript Community Minna which opinion weigh heavily on me and decided to make this article about them. When everyone agreed that constructor functions and classes are the same a friend of mine raised a suggestion and say

“Not totally. Classes now have support for truly private properties; you don’t get that with constructor functions”. (The code Genin)

more over “Class is more OOP (Object Oriented Programming) for instance if you write other OOP based languages like vue js” (Tomisinteazer) I suddenly drop the idea of ES6 is better than ES5 but rather saw in a different light that it more about productivity and clean code.

“In a more different light functions and classes isn’t totally the same because there is set and get method” (The Code Genn).

Another heat was drop on that comment get and set you can always write it yourself it syntactic sugar” (Tomisinteazer). The chat was hot and mild but the ideas kept on coming and it was pointing to one direction Learn both.

If you are to get an interview and you get question that would really helped you buy learning about the basic, what would you say. JavaScript is more than learning the latest technology or syntax it requires proper knowledge of archaic and deprecated syntax it never hurt to learn them.

“Prototypal inheritance is what the class syntax does under the hood. It just make it look like class based inheritance of other languages. I don’t know how class based inheritance works by the way, more like a shallow clone of object”. (Debo).

Finally constructor function and classes might look similar, one might look simple and easier but the paradigm of this syntax never vary. The modern javaScript and anything new is accepted, however unlike html and css nothing deprecate easily in javaScript it mostly have to deal with your professionalism and enthusiastic to work. You should keep building on previous knowledge and find a way to fit them in perfectly.

I hv perfected the art of taking my eyes off things I couldn’t afford call me weak, retarded bt I call it discipline cuz i will move a wall to get what is mine