Classes
Fields
A class is a plan for objects that share the same shape. Fields store the data of each object, and every field needs a clear type.
Give fields initial values so objects start in a valid state before any method runs.
class User { name: string = "Guest"; age: number = 0; } const user = new User(); console.log(user.name, user.age);
Guest 0
Methods
Methods are functions that live inside a class. They read field values and define what objects of that class can do.
Declare parameter types and a return type so bad calls are caught before the code runs.
class Greeter { greet(name: string): string { return "Hello " + name; } } const greeter = new Greeter(); console.log(greeter.greet("Ada"));
Hello Ada
What this is
Inside a method, this points to the current object. It lets the method read and update fields of the same object that received the call.
class Counter { total: number = 0; add(step: number): void { this.total = this.total + step; } } const counter = new Counter(); counter.add(2); counter.add(3); console.log(counter.total);
5
new instances
Use new to build an object from a class. Each call creates an independent object with its own copy of field values.
class User { name: string = "Guest"; } const first = new User(); const second = new User(); first.name = "Ada"; console.log(first.name); console.log(second.name);
Ada
Guest
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Why does every field of a class need a type and usually an initial value?
Answer
The type is checked on every use, and the value makes a new object valid at once
The type keeps the field honest, and the starting value means no method meets an empty object.
-
What does
thispoint to inside a method?Answer
The object that received the call
Each call binds
thisto its own instance, so fields of that object are read and updated. -
What does each
new User()call produce?Answer
An independent object with its own field values
Every call builds a fresh instance, which is why changing one leaves the other alone.
-
What does this program print?
class User { name: string = "Guest"; age: number = 0; } const user = new User(); console.log(user.name, user.age);Answer
Guest 0
A new object starts with the declared values, and
console.logprints both with a space. -
What does this program print?
class Greeter { greet(name: string): string { return "Hello " + name; } } const greeter = new Greeter(); console.log(greeter.greet("Ada"));Answer
Hello Ada
The method returns the joined text, and the call prints it.
-
What does this program print?
class Counter { total: number = 0; add(step: number): void { this.total = this.total + step; } } const counter = new Counter(); counter.add(2); counter.add(3); console.log(counter.total);Answer
5
Both calls update the same object, so the field ends at five.
-
What does this program print?
class User { name: string = "Guest"; } const first = new User(); const second = new User(); first.name = "Ada"; console.log(first.name); console.log(second.name);Answer
Ada / Guest
The two objects are independent, so only the first one changed.
-
What does the compiler say about this program?
class User { name: string = "Guest"; } const user = new User(); user.name = 42; console.log(user.name);Answer
error TS2322: Type 'number' is not assignable to type 'string'.
The field is typed as a string, so a number cannot replace its value.
-
What does the compiler say about this program?
class Greeter { greet(name: string): string { return "Hello " + name; } } console.log(new Greeter().hello("Ada"));Answer
error TS2339: Property 'hello' does not exist on type 'Greeter'.
The class declares
greet, so a call to another name is not part of its shape. -
What does the compiler say about this program?
class Greeter { greet(name: string): string { return "Hello " + name; } } console.log(new Greeter().greet());Answer
error TS2554: Expected 1 arguments, but got 0.
The method needs one string, and the call passes none.
Article author: Arthur Isaev