Junior
What is the difference between a class description and a class instance?
sobes.tech AI
Answer from AI
Class description is a template or schema for creating objects. It defines the structure (properties/fields) and behavior (methods) of objects.
An instance of a class is a specific object created based on the class description. It has the structure and behavior defined in the class, but with its own unique data in its properties.
Example in JavaScript:
// Class description
class Car {
constructor(make, model) {
this.make = make;
this.model = model;
}
displayInfo() {
console.log(`This is a ${this.make} ${this.model}.`);
}
}
// Class instances
const myCar = new Car('Toyota', 'Corolla'); // Instance 1
const anotherCar = new Car('Honda', 'Civic'); // Instance 2
// Using instances
myCar.displayInfo(); // Outputs "This is a Toyota Corolla."
anotherCar.displayInfo(); // Outputs "This is a Honda Civic."