One blueprint, many objects

INPUT · Slides

Making a real one from the blueprint

01 / 04

From the mould to the real thing

Having a waffle iron does not make a waffle until you pour the batter in. A class is the same: making a real one is a separate move.

That move is new. A real one brought into being this way is called an instance.

02 / 04

Write new ClassName()

The class name goes after new, then a ( ). Put the instance you get into a variable and use it from there.

The inside is still empty. But an instance is one of the object family, so you can add properties later. This is the same writing you did in chapter 2.

class Card {}const card = new Card();card.title = "Star Map";console.log(card.title);

Result

Star Map

03 / 04

As many as you like from one blueprint

Instances made from the same class are separate containers. Change a property on one and the other stays as it was.

The same feeling as one waffle iron and separate waffles coming off it.

class Card {}const a = new Card();a.title = "Star Map";const b = new Card();b.title = "Moon Flute";console.log(a.title);console.log(b.title);

Result

Star Map
Moon Flute

04 / 04

You are still adding by hand

Right now you write .title = ... by hand after the new. Five properties means five lines, and ten instances means fifty.

The machinery for "putting the contents in as you make it" comes next lesson. First get this much into your hands. Off you go.