Understanding Classes in Javascript

(and in Object Oriented Programming in general)

Integers, floats, strings, arrays, objects, functions, loops, scope? Piece of cake. Classes? Not so easy.

Understanding what classes are, how they work, and how to incorporate them into your code is critical to developing any working enterprise application. Even more importantly, however, developing a better mental model and appreciation for classes will transform you from a mere programming hobbyist to a software craftsman demigod.

Let’s get to it.

Every data structure, including all the ones listed above, is a class. Programming languages each come with your basic and fundamental class types, like numbers, strings, arrays, functions, and–for most modern languages at least–objects. Beyond these fundamental types, however, you can use classes to create your own customized data type. You can make this customized data type store whatever bits of information you need, in whatever format you want.

“Why would you I want to create a new class?” you grumble under your breath. “All I need are the fundamental data types,” you explain further: “I can already store anything I could possibly want in them.”

The reason, my wary but intrepid friend, is twofold: scalability and complexity.

  1. First, there’s the issue of scalability. As your application grows in size, you will have thousands, maybe billions, of variables, containing multiple data types per user, per feature, per environment (development vs testing vs production), per date, and even also per software release version.

    Classes are going to serve as neat, clean, and well labeled shelves to put all your various related data into a much more organized, retrievable, and maintainable structure. Without this structure, your application might work as an MVP or prototype but would not be able to scale.

  2. Then there’s complexity. The world of data structures is so deep and profound, and you’ve only scratched the surface. To create linked lists, hash tables, components in React, binary search trees, and so many more interesting and complex data structures, we need classes. Without classes, we would not be able to have a working mental model of what those data structures do, nor how their instantiations (i.e. variables of a particular class) behave in the wild.

    A class allows you to wrap your mind around a specific data type so that you’re able to not only apply that data type to its intended use case, but to also be able to troubleshoot when an instantiation of that class is misbehaving. Think of a class as a chunk of several subchunks of data types (if that analogy didn’t make sense, read this).

So, why are classes important in object oriented programming (OOP)? They bring scalability and complexity to our application.

Classes provide identity and function to data. In other words, classes allow our data to have well defined jobs. To have a functioning society, everybody needs to have a role to play–right? The better defined and specialized that role is, the more valuable each member becomes.

The same applies to our data in our application. Indeed, the fact that you need more, better, or more defined classes is usually a sign that your application is growing and becoming more competent and specialized.

Vahid Dejwakh
Vahid Dejwakh
Software Engineer at Microsoft;
Lover of good music and poetry

Vahid writes about interesting ideas at the intersection of software, system design, data, philosophy, psychology, policy, and business. He enjoys coffee and has a palate for spicy and diverse foods.

comments powered by Disqus

Related