One blueprint, many objects

INPUT · Slides

A class is a blueprint for one shape

01 / 04

You do not want to write the same shape over and over

Last lesson you lined objects of the same shape up by hand.

Two is fine. But what about twenty? And if halfway through you decide "I want a price on them too", that is twenty places to fix.

What you want is a way of saying "make one like this" just once.

02 / 04

Write the blueprint first

That way is a class. Write class Name { } and you have one blueprint saying this is the shape of thing I make.

We will be filling the inside in from here, so an empty { } is fine for now. Even that much is not an error.

class Card {}console.log("Card blueprint");

Result

Card blueprint

03 / 04

The name starts with a capital

It is the convention that a class name starts with a capital. So that card the container variable and Card the blueprint can be told apart by eye.

Make the name what you call one of them. Even for a blueprint that turns out a whole pile of cards, it is Card, not Cards.

class Card {}class Monster {}console.log("two blueprints made");

Result

two blueprints made

04 / 04

A blueprint is not yet a real one

Stare at a waffle iron all you like and no waffle comes out. A class is the same: writing one does not bring a single piece of real data into being.

The steps for making a real one from the mould come next lesson.

This round is practice at writing blueprints. Since you cannot make real ones yet, show data of that shape as a hand-written object. Off you go.