TL ; DR:
An object is an instance of a class — a real, usable copy made from the blueprint.
You create one with the
newkeyword:let rex = new Dog();
Each call to
newcreates a separate object, even if they all come from the same class.
Full lesson:
In the previous lesson you defined the Dog class — the blueprint. But a blueprint isn't a dog. To get an actual dog you can interact with, you need to create an instance of the class. An instance is also called an object.
The new keyword
To create a new object in TypeScript, use the new keyword followed by the class name and parentheses:
let rex = new Dog();
What this line does:
new Dog()creates a freshDogobject.let rexdeclares a variable namedrexthat holds the new object.The
=connects the two —rexnow refers to the freshly-created dog.
Many objects from one class
You can call new Dog() as many times as you want, and each call gives you a brand-new, independent object:
let rex = new Dog();
let buddy = new Dog();
let luna = new Dog();
rex, buddy, and luna are all Dog objects, but they're different objects. Changing one won't affect the others — they share the blueprint but live their own lives.
Your task
The Dog class is already defined for you. Below it, create three separate Dog objects named rex, buddy, and luna. The pre-filled console.log will confirm it by printing:
rex exists
buddy exists
luna existsFree plan: 30 tutor messages. PRO is unlimited.
