DeveloperBreeze

JavaScript Class with Constructor and Method

javascript
// 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();

Related Posts

More content you might like

Tutorial
javascript

Understanding JavaScript Classes

This tutorial assumes a basic understanding of JavaScript, including functions, objects, and inheritance. If you're new to these concepts, it may be helpful to review them before proceeding.

JavaScript classes are essentially syntactic sugar over JavaScript’s existing prototype-based inheritance. They provide a cleaner and more intuitive way to create objects and handle inheritance.

Sep 02, 2024
Read More
Code
php

PHP Class and Object Example

No preview available for this content.

Jan 26, 2024
Read More
Code
python

Class with Method

No preview available for this content.

Jan 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Be the first to share your thoughts!