// 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
- JavaScript Classes Documentation - MDN
- Object-Oriented Programming in JavaScript
- Understanding ECMAScript 6 - Classes
This tutorial provides a comprehensive guide to understanding and using JavaScript classes, from the basics to more advanced features, enabling you to write cleaner and more maintainable code in your JavaScript applications.
Sep 02, 2024
Discussion 0
Please sign in to join the discussion.
No comments yet. Start the discussion!