🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·
JavaScript

Classes

📚 What are JavaScript Classes? A class is a blueprint for creating objects. You define properties and methods once in the class, then create as many instances as you need with new. Classes organise related data and behaviour together, making complex code much easier to manage.

8 min 10 XP Lesson 17 of 21
Classes
🌐

Appy Says…

What if you could create a blueprint for 'User' and then stamp out a thousand users instantly — each with their own name, score, and methods? That's exactly what classes do. They're the building blocks of large JavaScript applications.

📖

What are JavaScript Classes?

A class is a blueprint for creating objects with shared properties and methods. You new the class to create an instance. Classes in JavaScript are syntactic sugar over prototype-based inheritance.

  • Define: class User { constructor(name) { this.name = name; } }
  • Instantiate: const u = new User('Appy');
  • constructor() runs when you create a new instance
  • this refers to the current instance inside methods
  • Add methods: greet() { return 'Hi, ' + this.name; }
  • extends: class Admin extends User { ... } — inherit from a parent class
  • super() — call the parent class constructor
🎮

Think of it like a Roblox character template

In Roblox, you can create a character template with health, speed, and a jump method. Every new character spawned from that template has the same setup — but each instance has its own health value. The class is the template; each new User() is a freshly spawned character.

⚙️

How It Works

  • 1. class Dog { constructor(name) { this.name = name; } bark() { return 'Woof!'; } }
  • 2. const rex = new Dog('Rex'); — creates a new instance, runs constructor
  • 3. rex.name'Rex'; rex.bark()'Woof!'
  • 4. Methods are shared on the prototype — not duplicated per instance (memory efficient)
  • 5. class Poodle extends Dog { fetch() { return this.name + ' fetches!'; } }
  • 6. static methods belong to the class itself, not instances: User.create()
🌍

Real-World Examples

  • A game: class Enemy { constructor(health) { this.health = health; } }
  • A chat app: class Message { constructor(text, author, timestamp) { ... } }
  • React class components (legacy but important to recognise): class App extends React.Component { ... }
  • Node.js EventEmitter — your custom classes can extend EventEmitter to emit events
  • TypeScript heavily uses classes for typed service layers and data models
💡

Key Facts

  • JavaScript classes were added in ES6 (2015) — before that, developers used constructor functions manually
  • Under the hood, classes use the prototype chainclass is syntactic sugar
  • instanceof checks: rex instanceof Dogtrue
  • Private fields (ES2022): #score = 0; — only accessible inside the class
⚠️

Watch Out!

Forgetting new when creating an instance is a classic bug. User('Appy') calls the class as a function, which in strict mode throws a TypeError. In non-strict mode it silently creates global variables. Always use new.

📌

Remember

Classes are blueprints. new ClassName() creates an instance. this inside methods refers to that instance. Methods are shared on the prototype.

What You Learned

  • Classes define blueprints; new ClassName() creates instances with their own data
  • constructor() initialises, methods are shared, extends enables inheritance
  • Unlocks: organising complex code, game entities, React class components, TypeScript models

Key Facts

  • JavaScript classes were added in ES6 (2015) — before that, developers used constructor functions manually
  • Under the hood, classes use the prototype chainclass is syntactic sugar
  • instanceof checks: rex instanceof Dogtrue
  • Private fields (ES2022): #score = 0; — only accessible inside the class

Real-World Examples

• A game: <code>class Enemy { constructor(health) { this.health = health; } }</code> • A chat app: <code>class Message { constructor(text, author, timestamp) { ... } }</code> • React class components (legacy but important to recognise): <code>class App extends React.Component { ... }</code> • Node.js EventEmitter — your custom classes can <code>extend EventEmitter</code> to emit events • TypeScript heavily uses classes for typed service layers and data models

Remember

Classes are blueprints. new ClassName() creates an instance. this inside methods refers to that instance. Methods are shared on the prototype.

Quick Quiz

1 / 2

new ClassName() creates?