OOP (Object-Oriented Programming) — Constructors and Destructors
Constructors are special methods used to initialize objects when they are created. They are automatically called and set up the initial values of the object’s properties. Destructors are special methods used to clean up resources when an object is no longer needed. While not all programming languages have destructors, they are common in languages like C++ and help prevent memory leaks. Constructors make it easy to create well-prepared objects, and destructors ensure proper cleanup.
class Person {
constructor(name) {
this.name = name;
console.log(this.name + ' created');
}
}
let person1 = new Person('Alice');