Class Basics in JavaScript
ES2015 introduced class syntax as a cleaner, more familiar way to create objects and set up inheritance. Under the hood, JavaScript classes are syntactic sugar over the prototype-based inheritance system, but they provide a much clearer structure for object-oriented code.
The class Keyword
Define a class with the class keyword followed by the class name. Class names conventionally use PascalCase. The class body contains method definitions using the concise method syntax — no commas between methods, unlike object literals.
constructor()
The constructor method is called automatically when you create a new instance with new ClassName(). Use it to initialise instance properties. A class can have at most one constructor.
Instance Methods
Methods defined in the class body are added to the prototype, meaning all instances share a single copy of each method — efficient regardless of how many instances you create.