Cheat Sheet

Use this as a reference while working.

Table of Contents:

The four pillars of OOP:

  1. Encapsulation — bundling data with the methods that operate on it

  2. Abstraction — creating interfaces that hide complexity

  3. Inheritance — sharing behavior between classes via extends

  4. Polymorphism — different objects responding to the same method in different ways

Encapsulation and this

Functional vs. OOP Approach

In a functional approach, data and functions are separate:

With encapsulation, data and the methods that operate on it are bundled together:

Factory Functions

A factory function creates and returns objects with a shared interface:

The this Keyword

this refers to the object that is calling the method:

Arrow functions do NOT bind their own this — they inherit this from their parent scope. Use function shorthand (method() {}) for object methods, not arrow functions.

Closures and Data Hiding

Closures

A closure is created when an inner function maintains a reference to variables from an outer function's scope, even after the outer function has returned:

Data Hiding with Closures

Data hiding limits access to data to prevent invalid modifications. Use closures to make data private and expose controlled access through methods:

Always return copies of arrays and objects to prevent external mutation.

Classes

Class Basics

A class is a blueprint for creating objects with shared interfaces. Use the new keyword to create instances.

  • constructor() runs when new is called

  • Properties set with this.x = y in the constructor are own properties (unique to each instance)

  • Methods defined in the class body are shared via the prototype

Memory Efficiency

Classes are more memory-efficient than factory functions because methods are shared on the prototype, not duplicated per instance:

Private Properties and Static Methods

Private Fields

Use # to make properties and methods private (accessible only inside the class):

When to use private: passwords, sensitive data, data requiring validation, internal helper methods.

Static Properties and Methods

static properties and methods belong to the class itself, not to instances:

When to use static: constants shared across instances, tracking all instances, factory/utility methods.

Inheritance

Inheritance lets a subclass extend a superclass, inheriting its methods and properties. Use extends and super.

Prototype chain: studentStudent.prototypePerson.prototypeObject.prototypenull

  • super() in the constructor calls the parent's constructor()

  • super.method() calls the parent's version of an overridden method

Polymorphism

Polymorphism means different objects can be treated the same way when they share the same method names, even if the implementations differ:

Duck typing — objects don't need to share a parent class, they just need to have the same method name:

System Design and UML Diagrams

System design is the process of identifying the entities (classes), their responsibilities, and their relationships.

Key questions to ask:

  1. What are the entities (classes)?

  2. What are their responsibilities (properties and methods)?

  3. What are the relationships between entities?

UML Class Diagram Notation:

Relationship multiplicity:

  • 1 — exactly one

  • 0..* — zero or more

  • 1..* — one or more

System Design Patterns

The Coordinator Pattern

One class manages interactions between multiple entities:

Use when one entity coordinates others, stores collections, validates interactions, and enforces business rules.

Status-Driven Behavior

Objects maintain internal state that controls what actions they can perform:

Use when objects go through stages (pending → approved/rejected) and certain actions are only valid in certain states.

The Intermediary Object Pattern

A new class represents the relationship between two entities, enabling many-to-many relationships:

Use when two entities have a many-to-many relationship and the relationship itself has data (grade, date, status).

Common Design Decisions

Decision
Guideline

Private vs. Public

Default to private for data with business logic; public for simple data

Uni- vs. Bi-directional

One-way when only one direction is queried; two-way when both are needed

Creator Responsibility

The entity that initiates an action should create the object

Last updated