Skip to content

Class

A class is a blueprint or template for creating objects. It defines what properties (data) and methods (behavior) the objects created from it will have.

Think of a class as a cookie cutter and objects as the cookies. The cookie cutter defines the shape, but each cookie is a separate instance.

  • Provides a template so you don’t write the same code repeatedly
  • Organizes code by grouping related properties and methods
  • Reusability — One class can create many objects
  • Maintainability — Change the class, all objects get the update
  • To model real-world entities (User, Product, Order)
  • To create multiple instances with shared behavior but independent data
  • To organize code in a logical, hierarchical way

ConceptAnalogy
ClassThe blueprint/architectural plan for a house
Blueprint DetailsSpecifies rooms (properties), plumbing/electrical (methods)
Object (Instance)The actual physical house built from that blueprint
Multiple ObjectsMany houses can be built from the same blueprint

A blueprint is not a house — it’s the idea of a house. Similarly, a class is not an object — it’s the definition of an object.


A class is a user-defined data type that serves as a template for creating objects. It encapsulates data (fields/properties) and functions (methods) that operate on that data.


Without classes, you’d have to manually create each object:

// Without a class — repetitive and error-prone
const car1 = { brand: "Toyota", color: "Red", start() { /* ... */ } };
const car2 = { brand: "Honda", color: "Blue", start() { /* ... */ } };
const car3 = { brand: "Ford", color: "Black", start() { /* ... */ } };

With a class:

class Car {
constructor(brand, color) {
this.brand = brand;
this.color = color;
}
start() {
console.log(`${this.brand} is starting...`);
}
}
const car1 = new Car("Toyota", "Red");
const car2 = new Car("Honda", "Blue");
const car3 = new Car("Ford", "Black");

classDiagram
class Car {
+brand: string
+color: string
+year: number
+start() void
+stop() void
+accelerate() void
}
class CarInstance1 {
brand: \"Toyota\"
color: \"Red\"
year: 2023
}
class CarInstance2 {
brand: \"Honda\"
color: \"Blue\"
year: 2024
}
Car --> CarInstance1 : instance of
Car --> CarInstance2 : instance of

// ES6 Class Syntax
class Person {
// Constructor — called when creating a new instance
constructor(name, age) {
this.name = name;
this.age = age;
}
// Method
greet() {
return `Hi, I'm ${this.name} and I'm ${this.age} years old.`;
}
// Getter
get birthYear() {
return new Date().getFullYear() - this.age;
}
// Setter
set birthYear(year) {
this.age = new Date().getFullYear() - year;
}
// Static method — called on the class itself, not instances
static createAnonymous() {
return new Person("Anonymous", 0);
}
}
const john = new Person("John", 30);
console.log(john.greet()); // "Hi, I'm John and I'm 30 years old."
console.log(john.birthYear); // 1994
john.birthYear = 1990;
console.log(john.age); // 34
const anonymous = Person.createAnonymous();

ComponentDescriptionExample
Fields/PropertiesData the object holdsname, age, color
ConstructorSpecial method to initialize objectsconstructor(name, age)
MethodsFunctions that define behaviorgreet(), start()
Getters/SettersControlled access to propertiesget fullName()
Static MembersBelong to the class, not instancesMath.PI, Array.from()
Access ModifiersControl visibilitypublic, private, protected

classDiagram
class ClassStructure {
+Fields: Properties/Data
+Constructor: Initialization
+Methods: Behavior/Functions
+Getters/Setters: Access Control
+Static Members: Class-level
-Private Fields: Hidden Data
}
class ObjectInstance {
+Field Values: \"John\", 30
+Method Calls: .greet()
}
ClassStructure --> ObjectInstance : creates

  • Single Responsibility — A class should have one reason to change
  • Meaningful Names — Name classes as nouns (User, Payment, Order)
  • Keep it Focused — Don’t create “God classes” that do everything
  • Favor Composition — Prefer combining small classes over large inheritance
  • Limit Constructor Work — Constructors should only initialize, not do logic
  • Use Static Members Wisely — Only for truly class-level behavior

  • Doing too much in the constructor — Constructors should set up, not process
  • Too many parameters — If a constructor needs 6+ parameters, consider a builder pattern
  • Public fields by default — Always consider making fields private first
  • Classes without a clear purpose — If you can’t describe it in one sentence, it’s too broad
  • Mixing concerns — A class shouldn’t handle both database and presentation logic

Easy:

  1. What is a class in OOP?
  2. What’s the difference between a class and an object?
  3. What are the main components of a class?

Medium: 4. What is a constructor and why is it important? 5. What are static methods and when would you use them? 6. How do access modifiers (public, private, protected) work?

Advanced: 7. Explain the concept of “class-level” vs “instance-level” members. 8. How does class inheritance affect the constructor chain? 9. In JavaScript, how does the class syntax differ from prototype-based inheritance under the hood?


ConceptKey Point
ClassBlueprint/template for creating objects
FieldsData that each object will hold
MethodsBehavior that each object can perform
ConstructorSpecial method to initialize new objects
StaticBelongs to class, not instances
One class = one responsibilityKeep classes focused and small

Previous Topic: Procedural vs OOP → Next Topic: Object → Related Topics: Constructor →, Static Members →