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 john = new Person('John', 30);
john.greet(); // Output: Hello, my name is John and I am 30 years old.
The constructor
method is a special method that is automatically called when an instance of the class is created. It's typically used to initialize properties of the class.