What is the ES6 class Syntax?
Introduced in ES6 (2015), the
class keyword is a cleaner way to write object-orientated code in JavaScript. It still uses prototypes under the hood, but it's much easier to read and maintain for developers coming from languages like Java or C#.javascript
Inheritance with extends & super
You can create a subclass that inherits methods and properties from a superclass (parent class).
extends: Links the child class to the parent.super(): Calls the parent's constructor! (Must be the first line!).
javascript
Creating Private Properties (#)
In the past, we used an underscore (
_name) as a convention to mark properties as "private." Modern JavaScript now has true private fields using the hash (#) prefix. These cannot be accessed outside the class!javascript
Static Methods & Properties
A static member belongs to the class itself, not to individual objects created from it.
javascript
Summary
| Feature | Description |
|---|---|
| constructor | Special method to initialize data |
| extends | Derive one class from another |
| super() | Call parent constructor or method |
| #private | Truly private fields (cannot be read outside the class) |
| static | Properties/methods belonging to the CLASS, not instance |
| Best For | Structuring code, complex business logic, UI components |
| Sugar | It's still prototypes under the hood! |
| this | Refers to the current object instance |
| get / set | Special methods for accessing/updating data with logic |
| instanceof | Verify if an object belongs to a class |
| Inheritance | Promotes code reuse and modularity |
| Class Form | Always uses strict mode even if not specified |
| Hoisting | Unlike functions, classes are NOT hoisted! |
| Bound Method | Use arrow functions inside classes to preserve this |
| Factory | Use static methods for creating custom instances |