Docs
Module-11-Classes
Module 11: Classes
Overview
This module provides comprehensive coverage of ES6+ class syntax in JavaScript. While classes are syntactic sugar over JavaScript's prototype-based inheritance, they provide a cleaner, more intuitive way to create objects and implement inheritance patterns.
Learning Objectives
By the end of this module, you will be able to:
- ā¢Define classes using declaration and expression syntax
- ā¢Implement constructors and instance methods
- ā¢Use getters and setters for computed properties
- ā¢Extend classes using inheritance
- ā¢Override methods and call parent methods with
super - ā¢Create and use static properties and methods
- ā¢Implement private fields and methods (ES2022)
- ā¢Apply common class-based design patterns
Prerequisites
- ā¢Module 10: Objects (object basics and prototypes)
- ā¢Module 5: Functions (closures, this binding)
Sections
11.1 Class Basics
- ā¢Class declarations vs expressions
- ā¢Constructor method
- ā¢Instance properties and methods
- ā¢Getters and setters
- ā¢Method chaining
11.2 Class Inheritance
- ā¢The
extendskeyword - ā¢Super constructor calls
- ā¢Method overriding
- ā¢Prototype chain in classes
- ā¢Multi-level inheritance
11.3 Static Members
- ā¢Static properties
- ā¢Static methods
- ā¢Static blocks
- ā¢Factory methods
- ā¢Utility classes
11.4 Private Fields
- ā¢Private instance fields (
#) - ā¢Private methods
- ā¢Private static members
- ā¢Encapsulation patterns
- ā¢ES2022 private syntax
11.5 Class Patterns
- ā¢Mixins and composition
- ā¢Factory pattern
- ā¢Singleton pattern
- ā¢Builder pattern
- ā¢Decorator pattern
Class vs Prototype Comparison
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā ES6 Class vs Prototype Pattern ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā ā
ā // ES6 Class Syntax // Prototype Pattern ā
ā class Person { function Person(name) { ā
ā constructor(name) { this.name = name; ā
ā this.name = name; } ā
ā } ā
ā Person.prototype.greet = ā
ā greet() { function() { ā
ā return `Hi, ${this.name}`; return `Hi, ${this.name}`ā
ā } }; ā
ā ā
ā static create(name) { Person.create = function(name) {ā
ā return new Person(name); return new Person(name); ā
ā } }; ā
ā } ā
ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
Key Concepts
| Concept | Description | Example |
|---|---|---|
class | Declares a class | class Animal {} |
constructor | Initializes instances | constructor(name) {} |
extends | Inherits from parent | class Dog extends Animal |
super | Calls parent class | super(name) |
static | Class-level member | static count = 0 |
# | Private field | #secret = 'hidden' |
get/set | Property accessors | get name() {} |
File Structure
Module-11-Classes/
āāā README.md
āāā 11.1-Class-Basics/
ā āāā README.md
ā āāā examples.js
ā āāā exercises.js
āāā 11.2-Class-Inheritance/
ā āāā README.md
ā āāā examples.js
ā āāā exercises.js
āāā 11.3-Static-Members/
ā āāā README.md
ā āāā examples.js
ā āāā exercises.js
āāā 11.4-Private-Fields/
ā āāā README.md
ā āāā examples.js
ā āāā exercises.js
āāā 11.5-Class-Patterns/
āāā README.md
āāā examples.js
āāā exercises.js
Best Practices
- ā¢Use classes for OOP - Cleaner syntax than prototype manipulation
- ā¢Prefer composition - Mixins over deep inheritance hierarchies
- ā¢Encapsulate state - Use private fields for internal data
- ā¢Keep classes focused - Single responsibility principle
- ā¢Document public API - Clear method signatures and purpose
- ā¢Use static for utilities - Factory methods, helper functions
- ā¢Validate in setters - Ensure data integrity
Common Pitfalls
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā Watch Out For ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā ā
ā 1. Forgetting `new`: ā
ā const p = Person('Alice'); // ā TypeError ā
ā const p = new Person('Alice'); // ā
ā
ā ā
ā 2. Missing `super()` in subclass: ā
ā class Child extends Parent { ā
ā constructor() { ā
ā // super() must be called before 'this' ā
ā super(); ā
ā this.value = 1; ā
ā } ā
ā } ā
ā ā
ā 3. Arrow methods lose inheritance: ā
ā class A { ā
ā method = () => {}; // Can't be overridden properly ā
ā } ā
ā ā
ā 4. Private fields are truly private: ā
ā class A { #x = 1; } ā
ā const a = new A(); ā
ā a.#x; // ā SyntaxError ā
ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
Exercises Overview
Each section includes exercises covering:
- ā¢Basic class creation and instantiation
- ā¢Inheritance hierarchies
- ā¢Static member usage
- ā¢Private field encapsulation
- ā¢Design pattern implementation