Creating a Class

Deep dive into the basics of class creation in old and modern JavaScript.

JavaScript and object-oriented programming

Older versions of JavaScript supported classes but without using the class keyword. In the past, you may have found object-oriented programming (OOP) in JavaScript rather primitive, especially if you were familiar with other mainstream languages. Even today, many programmers still think that JavaScript has little to do with OO programming. One of the main reasons for that is that the old syntax and semantics of working with classes were very confusing and error prone. Modern JavaScript puts an end to that misery. Now, it is possible to create beautiful OOP code with JavaScript.

Classes in JS

Classes are the most fundamental part of OOP, and yet, earlier versions of JavaScript did not have an explicit keyword to define classes. It was never clear if we were working with a class or a function. Serious OOP requires more rigorous syntax and clearer specifications for creating and using classes. Fortunately, modern JavaScript delivers this quite well.

We’ll quickly revisit the ...