ES6 and JavaScript: Constructors

3 min readOct 9, 2020

JavaScript is not an object oriented language (OOP), but an object-based language. That is to say, everything which exists in JS is an object: classes, numbers, functions…are all {}.

By comparison, C++ an OOP language, to create an object, one must first instantiate a class and then implement a constructor which then creates an object.

An example of a C++ Class and Constructor

In this example is a class MyClass, which has an attribute of the integer myNum and string myString. However, an object doesn’t exist until the constructor int main() creates myObj from the class itself,MyClass myObj;and assigns values, myObj.myNum = 15; that any number can be returned, which is done at cout << myObj.myNum << "\n" to return the value of 15. The integer belongs to this.myObj, not MyClass.

Getting back to JavaScript; before 2015, everything was an object declared byvar; if an engineer working from an OOP language started coding in JS, expecting constructors they would get confused very quickly.

An object literal can be named using JavaScript:

An example of an object being rendered in HTML using property accessors, aka dot notation

person is an object, without a class; it is one instance of object which has all of its attributes (in this case we call them props or properties) assigned directly within curly braces…and when person.firstName is assigned to the screen via document.getElementById(“demo”).innerHTML a new <div> is made and the object person firstName is assigned as text.

The problem with this method is each object is unique and cannot be repeated through this method. We need to declared all the data explicitly.

This is very DRY 🏜

So how does JavaScript work without explicitly naming everything, since it doesn’t use an OOP method? ES6 allows us to create a Class, a function in JavaScript which is still an object, but it’s behavior is class-like, and it has a constructor feature.


Composer, Designer, Educator


See more recommendations