JavaScript Classes: Object-Oriented Programming
Master JavaScript classes. Learn class syntax, constructors, methods, inheritance, and OOP concepts.
JavaScript Classes
What are Classes?
Classes are blueprints for creating objects. They define properties and methods that objects will have.
``` ┌─────────────────────┐ │ Class │ │ (Blueprint) │ │ ┌───────────────┐ │ │ │ Properties │ │ │ │ Methods │ │ │ └───────────────┘ │ └──────────┬──────────┘ │ │ new ClassName() │ ┌───────────▼───────────┐ │ Object │ │ (Instance) │ └───────────────────────┘ ```
Class Syntax
```javascript class User { constructor(name, email) { this.name = name; this.email = email; } greet() { return `Hello, ${this.name}!`; } }
const user = new User('John', 'john@example.com'); user.greet(); // "Hello, John!" ```
Inheritance
```javascript class Admin extends User { constructor(name, email, role) { super(name, email); this.role = role; } deleteUser() { return 'User deleted'; } } ```
Key Takeaway
Classes create object blueprints. Use constructor for initialization. Extend classes for inheritance. Classes make code organized and reusable.