new with your own classes
`new` calls a class's constructor and gives you back a new object, an instance of that class.
A class is a mold. new Cart("guest") uses that mold to create a new object and passes "guest" to the constructor.
Read the line like this: new, the class name, and the arguments in parentheses. The result is an instance with the class's fields and methods. Each new creates a different object.
Without new, JavaScript throws a TypeError: a class can't be called like a normal function. That is why new is required.
The type checker also uses the class name as a type: const c: Cart = new Cart("guest"). That type is gone when Node runs the code; new and the class stay.
The course uses it in lessons like 01.07: return new Cart("guest");. A function creates and returns an instance ready to use.
const cart = new Cart("guest");
Reach for it when: you need a new object with the shape and behavior you defined in a class.
Examples
Common mistake
class Cart {
owner: string;
constructor(owner: string) {
this.owner = owner;
}
}
const cart = Cart("guest");Why: A class can't be called like a function. Without `new`, TypeScript rejects it (and in JavaScript it would be a `TypeError`).
How to write it: Write new Cart("guest").
