Classes — Complete Guide
Classes — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of MEAN Stack Tutorial on Toolliyo Academy.
On this page
MEAN Stack Tutorial · Lesson 23 of 100
Classes
Stack → Projects
Stack · 1 — Pieces · ~6 min · MEAN — TypeScript & RxJS
What is this?
TypeScript classes combine properties, methods, and constructors — used for Angular services, domain models, and Express class-based middleware.
Why should you care?
Classes encapsulate behavior — an Account class can format balance; a service class wraps HTTP.
See it live — copy this example
Paste into your MeanVerse project (Angular + Express + MongoDB), then run with ng serve / node / mongosh as noted.
export class Money {
constructor(public readonly cents: number, public readonly currency: string) {}
add(other: Money): Money {
if (this.currency !== other.currency) throw new Error('Currency mismatch');
return new Money(this.cents + other.cents, this.currency);
}
format(): string {
return new Intl.NumberFormat('en-US', { style: 'currency', currency: this.currency })
.format(this.cents / 100);
}
}
What happened?
- readonly prevents reassignment.
- add returns new Money — immutable pattern safe for concurrent updates.
- format hides Intl details from callers.
Practice next
- Model Money or Account as a class with methods.
- Use private fields for internal state.
- Inject class-based service in Angular with @Injectable.
- Implement subtract and equals on Money.
- Add static fromDollars(d: number) factory method.
Remember
Classes bundle data and behavior. Use for domain logic like Money, DateRange. Angular DI works naturally with classes.
Ledger precision
MeanVerse stores amounts in cents as integers inside Money class.
Outcome: Floating-point bugs avoided in transfer calculations.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!