ES6 and JavaScript: Constructors

MDN
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:

--

--