class Person { constructor(name, age) { this.name = name; this.age = age; }
greet() { console.log(`Hello, my name is {this.name} and I am {this.age} years old.`); } }
const alice = new Person('Alice', 30);
alice.greet();
In JavaScript, classes were introduced in ES6 (ECMAScript 2015) to provide a more familiar and... concise syntax for creating objects and handling inheritance compared to the traditional prototype-based approach. Classes in JavaScript are syntactic sugar over JavaScript's existing prototype-based inheritance. Here's a basic example of how to define and use a class in JavaScript Define a class named Person Constructor method to initialize the object with name and age properties Method to display information about the person Creating an instance of the Person class Calling the method on the created instance