Published on January 26, 2024By DeveloperBreeze

// Class definition for Person
class Person {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }

    // Method to generate a greeting
    greet() {
        return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
    }
}

// Usage of the Person class
const person = new Person('John', 30);
const greeting = person.greet();

Comments

Please log in to leave a comment.

Continue Reading:

Class with Method

Published on January 26, 2024

python

PHP Class and Object Example

Published on January 26, 2024

php

Understanding JavaScript Classes: A Tutorial

Published on September 02, 2024

javascript