Curriculum
Concept 58 // Module 06: Objects & Prototypal Inheritance
ES6 Classes & Class Syntax
Module 06: Objects & Prototypal Inheritance•Concept 58 of 100• 5 min read
ES6 Classes & Class Syntax
**Classes** (ES6) are 'syntactic sugar' over JS's existing prototypal inheritance. They provide a cleaner, more organized way to create blueprints, handle inheritance, and define methods.
The Core Concept
Syntax Reference
// 1. Definition
class User {
constructor(name) {
this.name = name;
}
// Method (lives on prototype)
sayHi() {
console.log("Hi " + this.name);
}
}
// 2. Inheritance
class Admin extends User {
constructor(name, level) {
super(name); // Call parent constructor
this.level = level;
}
}
Live Interactive Studio
Modern JS classes support **Private Fields** (using `#`). They cannot be accessed outside the class, providing true encapsulation.
JavaScript Editor
Loading...
Console Output
Waiting for output...
Key Code Patterns in Action
01
Public vs Private Fields
// 1. Public vs Private Fields
class Account {
balance = 0; // Public field
#pin = 1234; // Private field (# prefix)
deposit(val) {
this.balance += val;
}
getSecret() {
return this.#pin;
}
}
const myAcc = new Account();
console.log(myAcc.balance); // 0
// console.log(myAcc.#pin); // ❌ Syntax Error!Modern JS classes support **Private Fields** (using `#`). They cannot be accessed outside the class, providing true encapsulation.
02
Static Members
// 2. Static Members
class MathUtils {
static PI = 3.14;
static double(n) {
return n * 2;
}
}
console.log(MathUtils.PI); // 3.14
// const mu = new MathUtils();
// console.log(mu.PI); // undefined`static` properties and methods belong to the **Class itself**, not the instances. They are used for utility functions that don't need instance data.
03
Getters and Setters
// 3. Getters and Setters
class Person {
constructor(name) { this._name = name; }
get name() { return this._name.toUpperCase(); }
set name(val) { this._name = val; }
}
const p = new Person("subhajit");
console.log(p.name); // "SUBHAJIT"Getters and setters allow you to run logic when a property is accessed or modified, while keeping the external API looking like a simple property.
04
1. Core Usage: ES6 Classes & Class Syntax
// Core syntax and usage for ES6 Classes & Class Syntax
const feature = 'es6-classes-class-syntax';
console.log('Concept 58:', feature);
console.log('Active:', true);Illustrates the fundamental invocation and syntax structure for ES6 Classes & Class Syntax.
05
2. Defensive Handling & Edge Case
// Edge-case defensive handling
function handleSafe(input) {
if (input === null || input === undefined) return 'safe-fallback';
return String(input).trim();
}
console.log(handleSafe('es6-classes-class-syntax'));
console.log(handleSafe(null));Shows how to write defensive, safe JavaScript code that handles nullish or unexpected inputs when working with ES6 Classes & Class Syntax.
06
3. Clean Architecture & Production Pattern
// Production-grade pattern for ES6 Classes & Class Syntax
const settings = Object.freeze({
id: 'es6-classes-class-syntax',
ready: true,
timestamp: Date.now()
});
console.log('Settings initialized:', settings.id, settings.ready);Demonstrates clean, immutable production code avoiding unintended state mutations.
Real-World Engineering Takeaways
Object-Oriented Programming (OOP): Building large, scalable applications with clear hierarchies.
UI Components: Defining parts of a web app (like in React or Web Components).
Data Buffers: Managing complex state or streaming data.
Encapsulation: Using private fields to protect sensitive logic or internal state.
Under the Hood: Engine & Memory Mechanics
How JavaScript Executes This
### **Class Memory Layout**
**1. Function Object:**
- In the **Heap**, a Class is actually stored as a **Function object**.
- Methods defined inside the class are automatically attached to the Class's `prototype` object.
**2. Private Fields Storage:**
- Private fields (`#`) are stored in a hidden **WeakMap-like** internal storage in the engine.
- They do not show up when you log the object or use `Object.keys()`, making them invisible to the rest of the **RAM**.
**3. Inheritance Chain:**
- When a class `extends` another, the engine creates a link between the child Class object and the parent Class object.
- **Hardware:** The `super()` call handles the logic of initializing the parent's memory layout before the child's.
Quick Knowledge Check
Test your understanding on these 10 questions (one by one):
Score: 0 / 10
Question 1 of 10 • Core Purpose & Mental Model
What is the core purpose of ES6 Classes & Class Syntax in JavaScript?
// Concept 58: ES6 Classes & Class Syntax
// Understanding the core mental model1 / 10