// 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();JavaScript Class with Constructor and Method
javascript
Related Posts
More content you might like
Tutorial
javascript
Understanding JavaScript Classes
Static methods and properties belong to the class itself, rather than to instances of the class. They are often used for utility functions that are related to the class but don't need to operate on instances of the class.
class MathUtils {
static add(a, b) {
return a + b;
}
}
console.log(MathUtils.add(5, 3)); // Output: 8Sep 02, 2024
Read MoreDiscussion 0
Please sign in to join the discussion.
No comments yet. Be the first to share your thoughts!