Instantiation

You may have noticed that we used the ‘new’ keyword a couple times in our previous code:

Animal dog = new Animal();
Chair chair = new Chair();

The ‘new’ keyword is used for a process called “instantiation.” Classes are like “blueprints” for objects. The objects are the “instances” of the class. In the two lines of code above, ‘dog’ is the object instance of the ‘Animal’ blueprint (class) and ‘chair’ is an instance of the ‘Chair’ blueprint. Since we can have multiple “instances” of the class, this enables us to create multiple dogs with different attributes, such as dogs with different names, different weights, descending from different breeds, and so on.

To recap: the ‘class’ keyword creates the blueprint, and the ‘new’ keyword creates an object/instance of that blueprint.

JS++ | Classes, OOP, and User-defined Types

Up until now, we’ve been declaring variables, looping over data, and writing ‘if’ and other conditional statements. These operations comprise the “imperative programming” paradigm where we describe “how” a program operates step by step (or, more specifically, statement by statement).

Oftentimes, in real-world programming, we want to be able to describe real-world objects. For example, in an employee database, we need to be able to describe “employees” – and maybe even specific types of employees such as programmers, managers, salespeople, and so on. In a video game, we need to be able to describe the hero, the enemies, bullets, weapons, equipment, and other items. This need gave rise to the object-oriented programming (OOP) paradigm, most notably with the C++ programming language (although OOP itself predates C++) and then extending to Java, Python, C#, PHP, Objective-C, Swift, and many other modern languages.

JS++ provides access to object-oriented programming (OOP) via classes, which also create user-defined types. JS++ is an extension of the JavaScript ECMAScript 3 specification, which did not provide class support. Furthermore, JS++ classes are unique from class systems and libraries that tried to add OOP to JavaScript, a language that is “dynamically-typed” – where data types are determined at runtime (as we explored previously). Instead, JS++ classes operate naturally with many compile-time concepts where static typing and static analysis are used (where data types and errors are analyzed and checked at compile time): nominal typing, method overloading, parametric polymorphism (generic programming), compile-time overload resolution, virtual methods, and so on. In this way, object-oriented programming in JS++ is very similar to the OOP available in C++, Java, and C#. Therefore, design patterns and best practices that were developed over decades of experience in the aforementioned programming languages, from banking to rocketry, are also immediately applicable to JS++.

In this chapter, we will introduce basic OOP in JS++ by creating animal objects that we will render to a web page. We will then progressively add interactivity to these animal objects to illustrate core OOP concepts. We will also cover the four fundamentals of OOP: abstraction, encapsulation, inheritance, and polymorphism.

Similar Reads

Classes and User-Defined Types

Classes are the basic foundation of object-oriented programming in JS++. We can declare a class using the ‘class’ keyword:...

Instantiation

You may have noticed that we used the ‘new’ keyword a couple times in our previous code:...

Setting Up Our OOP Project

We’re now going to begin our OOP project. We’ll create classes for different animals, render them to a web page, make them talk with speech bubbles, and more....