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).
- 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
name, #token
revokeAccess()
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():
- JS checks if
describeexists directly onadmin. - Not found? Follow the link to
Admin.prototype. - Not found? Follow up to
User.prototype. - Still not found? Check
Object.prototype→ if missing, returnundefined.
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!
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.jsimportsb.jsandb.jsimportsa.js, one module sees uninitializedundefinedbindings. Extract shared helpers/types into a thirdshared.jsfile!
Try It Yourself in the Playground
Test ES6 inheritance (extends / super), #private field encapsulation, and prototype verification live below:
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
What happens if external code tries to read or mutate account.#balance on a class with #balance = 0?
Where are methods declared inside class User { greet() {} } stored in memory?