71. Introduction to ES6 Classes
In ES6 (2015), the class syntax was introduced to make object-oriented code cleaner and easier for developers coming from languages like Java or Python. Crucially, the JS class syntax is just 'syntactic sugar'—behind the scenes, it still uses the constructor function and prototype system.
Class Definition
Classes contain a special method called constructor(), which runs when a new object instance is created.
javascript class Vehicle { constructor(make, model) { this.make = make; this.model = model; this.isRunning = false; } }
Creating Instances
Just like constructor functions, we use the new keyword.
javascript const truck = new Vehicle('Ford', 'F150'); const sedan = new Vehicle('Toyota', 'Camry');
console.log(truck.make); // Ford console.log(sedan.isRunning); // false
Recommendation: Use the class syntax for all modern OOP in JavaScript.