Basics
A class is a template for creating objects. It defines the properties and methods that will be shared by all objects created from the class. Here is a simple example of a class in JavaScript:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
sayHello() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
}
const person1 = new Person('John', 30);
const person2 = new Person('Jane', 28);
person1.sayHello(); // "Hello, my name is John and I am 30 years old."
person2.sayHello(); // "Hello, my name is Jane and I am 28 years old."
Last updated