1. Deep JavaScript Mechanics & Modern ES6+

Prototypes, ES6 Classes (#private) & ES Modules

Understand how JavaScript's prototype chain powers ES6 classes, protect internal state with #private fields, and organize multi-file apps cleanly with ES Modules (import/export).

3 min readUpdated 2026-12-01
In This Lesson
  • Trace property and method lookup along JavaScript's prototype chain
  • Write ES6 classes with constructors, getters/setters, inheritance, and true #private fields
  • Structure multi-file projects cleanly with named vs default ES Module exports

Even though modern JavaScript uses familiar class syntax, under the hood it isn't Java or C#—JavaScript is prototype-based. Understanding prototypes, true #private class fields, and ES Modules (ESM) gives you full command of large JS/TS codebases.

1. The Prototype Chain Mental Model

JavaScript Prototype Lookup Chain
admin instance

name, #token

Admin.prototype

revokeAccess()

User.prototype

describe()

When accessing admin.describe(), JS checks the instance first, then Admin.prototype, then User.prototype, then Object.prototype.

Every JavaScript object has a hidden link ([[Prototype]]) pointing to a template object. When you write admin.describe():

  1. JS checks if describe exists directly on admin.
  2. Not found? Follow the link to Admin.prototype.
  3. Not found? Follow up to User.prototype.
  4. Still not found? Check Object.prototype → if missing, return undefined.

Methods defined inside a class live on the prototype—meaning 10,000 User instances share one describe function in memory instead of duplicating it 10,000 times!

2. Modern ES6 Classes & True #private Fields

Before ES2022, developers prefixed internal properties with _balance as a polite convention—anyone could still mutate account._balance = -999. Prefixing a field with # enforces runtime privacy in the JavaScript engine itself!

javascript
class BankAccount {
  #balance = 0; // Truly private — SyntaxError if accessed outside this class!

  constructor(owner, initialBalance = 0) {
    this.owner = owner;
    if (initialBalance > 0) this.#balance = initialBalance;
  }

  deposit(amount) {
    if (amount <= 0) throw new Error("Amount must be positive");
    this.#balance += amount;
  }

  get balance() {
    return this.#balance;
  }
}

3. ES Modules (import / export): Named vs Default

Every browser and modern Node.js / TypeScript project uses ES Modules:

  • Named exports (Preferred in TS/React): Force consistent naming across files and enable better IDE autocomplete & tree-shaking:
    javascript
    // math.js
    export function sum(a, b) { return a + b; }
    export const TAX_RATE = 0.08;
    
    // app.js
    import { sum, TAX_RATE } from "./math.js";
  • Default export: export default class ApiClient {} — caller can rename it arbitrarily (import Whatever from "./client.js"), which makes refactoring harder in big teams.
  • Avoid circular imports: If a.js imports b.js and b.js imports a.js, one module sees uninitialized undefined bindings. Extract shared helpers/types into a third shared.js file!

Try It Yourself in the Playground

Test ES6 inheritance (extends / super), #private field encapsulation, and prototype verification live below:

JavaScriptTry It Live: Classes, #private Fields & Prototypes
Output

Click “Run Code” to execute this snippet right in your browser.

Mini-Challenge: Why is export function formatPrice(...) (named export) generally preferred over export default function(...) in TypeScript teams?

Quick Checkpoints

Check Your Understanding

What happens if external code tries to read or mutate account.#balance on a class with #balance = 0?

Check Your Understanding

Where are methods declared inside class User { greet() {} } stored in memory?