TypeScript Tutorial
TypeScript Object Oriented Programming
Objects bundle data and the functions that work on it. Classes are the blueprint.
Data and behavior together
So far a program has been variables at the top of the file and free functions that take those values. That works until related pieces scatter: a name in one variable, a score in another, a function that expects both in the right order.
Object oriented programming (OOP) groups those pieces. An object holds data and the functions that use that data. A class is the blueprint that names the type. You write the class once. Then you create as many objects of that type as you need with new.
Class vs object
The class is the plan. The object is a real value in memory. number is a type;let n: number = 3; makes a variable. Lamp is a type you define;const desk: Lamp = new Lamp(); makes an object.
| Class | Object | |
|---|---|---|
| What it is | A type you define | A value of that type |
| When it exists | In the source, as a blueprint | When the program creates it |
| How many | One definition | As many as you create |
| Example | class Lamp | new Lamp() |
| Analogy | The cookie cutter | A cookie |
A tiny class
Lamp has one field, on, and one method, toggle. The method belongs to the class. You call it on an object with the dot operator. This program creates one lamp, flips it, and prints the new state.
Example
class Lamp {
public on: boolean = false;
public toggle(): void {
this.on = !this.on;
}
}
const desk: Lamp = new Lamp();
desk.toggle();
if (desk.on) {
console.log("on");
} else {
console.log("off");
}new Lamp() constructs the object. Inside toggle, this.on is that object’s field. The next chapters explain methods, constructors, and access in detail. Here the point is the bundle: data and the function that works on it live in one type.
Compile this in /typescript/try. Call toggle twice and watch the printed state change.
The same idea without a class
You could keep a let deskOn: boolean and a free function. The class version keeps the flag and the flip next to each other. When you add a second lamp, you do not invent a second pair of names. You create another Lamp.
Example
let deskOn: boolean = false;
deskOn = !deskOn;
console.log(deskOn);That works for one flag. It does not scale when a lamp also needs a brightness, a room name, and three operations. The class is the type that holds those together.
What the next chapters cover
- Classes and objects — define a type, create two objects with
new, usepublicmembers. - Methods — functions that live on the class and run with the dot operator and
this. - Constructors — code that runs when an object is created, so fields start valid.
- Access specifiers —
public,private, andprotected.
Later chapters add encapsulation, inheritance with extends, and polymorphism withoverride. Those are still the same idea: a class names a type, objects are values of that type created with new.
Keep classes small
For this tutorial a class has a few fields and a few methods. Do not pack a whole program into one type. Name the class after the thing it models: Lamp, Player, Point. Next: how to write a class and create objects from it.