Class Construction in JavaScript and C++

My first foray into Object-Oriented Programming (OOP) was with C# and C++. I was drawn to it because of the file organization, and if you are a style nerd like me, the concept of method and variable declaration and definition housed in different locations was a geek out moment.

But then there is JavaScript. Sure, there is class construction and instantiation patterns, but it was in one file, in one location and my brain was lost. I am here to give you hope, because JavaScript is just as organized and efficient in its class construction as any other language. For all intents and purposes, we’ll look at it from the lens of comparison with C++.

Four Styles of Class Implementation

  1. Functional
  2. Functional-shared*
  3. Prototypal*
  4. Pseudoclassical*

* These style bear the most resemblance to class implementation in C++.

Functional-shared, Prototypal, and Pseudoclassical all work by housing methods in one place, and allowing objects to access those methods through the use of pointer references. These three styles resemble what we C++ users do in our development environments.

Constructors

Most of the time, the header file is where we would create a new class and declare the class variables and methods. Below is an example of a class constructor for an Hourly class with an Employee object found in a .header file

headerfile

The .cpp file requires that any declared variables and methods be defined. Through the use of the keyword this, we allow any object created to point to the variables.

cppfile

In JavaScript, the constructor is an uppercase word like var Stack or var Queue
Just like in C++, JavaScript also contains variable and method declarations and definitions. The main difference is that the use JavaScript requires the use of the Object.Create() method to create a new instance of the class in the same file since JavaScript does not support cpp files.

classConstructionJS

Lastly, the objects are created in the same manner by using the new keyword.

So why does this matter?

When I first learned programming, a professor once encouraged me to do so in a very methodical way. It helps to think of it a modular way. This is what works for me in any object-oriented language

  1. Declare the properties
  2. Declare the object either using Object.create(‘nameOfTheMethods ) or var obj = new ‘nameOfClass’
  3. Declare the methods using { } and define their use in the data set
  4. Decide how the methods manipulate the properties.
  5. Determine how the more data (or nodes) are added, deleted, and connected using the methods.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s