JavaScript, inside out · Chapter 29 of 51

Inheritance

By the end of this chapter you can build one class on top of another with extends and super, make your own error class, and choose composition when a class tree does not fit. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: The kitchen
  • 5 tasks
  • JavaScript
  • In the all labs plan

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.

Why you need this

Tapri now has a staff roster: riders, baristas, and Meera as manager. Ravi wants the shift screen to treat them all as staff, each with a name, a role and a describe method, while a rider also has an area and a barista a shift. The kitchen wants an error it can catch when ginger runs out. And Anjali brews chai, delivers on quiet afternoons and sometimes bills: which kind of staff is she?

With what you know so far, each kind would be a separate class with the name and role code copied into it. Fix a bug in one describe and the other three still have it. Linking the prototypes by hand, as in chapter 27, works but is easy to get wrong.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Rider: a kind of Staff, with extends and super

    Make Rider extend Staff. constructor(name, area) runs Staff's constructor with the name and the role 'rider', then sets area. A rider has exactly three own properties: name, role and area.

    new Rider('Arjun', 'Indiranagar') is an instance of Rider and of Staff, and its describe() gives 'Arjun (rider)'. describe must come from Staff: do not write a describe in Rider.

  2. Task 2

    Manager.describe: build on the staff version

    Write Manager's own describe(). It returns what Staff's describe gives for this manager, followed by a comma, a space, 'runs ' and the stall: new Manager('Meera', 'Tapri Indiranagar').describe() gives 'Meera (manager), runs Tapri Indiranagar'.

    It must call Staff's describe through super, not repeat its text: the check changes Staff's describe and expects the manager's to follow.

  3. Task 3 · interview question

    Interview: Barista, the constructor that throws

    Barista is broken now: new Barista('Ravi', 'morning') throws a ReferenceError. Fix its constructor so a barista is a Staff with exactly three own properties: name, role 'barista' and shift.

    new Barista('Ravi', 'morning').shift gives 'morning', and describe() gives 'Ravi (barista)'.

  4. Task 4

    OutOfStockError: an error that carries the item

    Make OutOfStockError a real Error by extending Error. new OutOfStockError('ginger') has the message 'ginger is out of stock', the name 'OutOfStockError' and the item 'ginger'.

    String(error) gives 'OutOfStockError: ginger is out of stock', it has a stack like every error, and a catch can recognise it as an OutOfStockError.

  5. Task 5

    makeStaffMember: staff built from abilities

    Write makeStaffMember(name, ...abilities). Return a new object with name, every method of every ability given (any number, even none), and a method can(action) that returns true when the member has a method with that name and false otherwise. can('name') is false, because name is not a method.

    makeStaffMember('Anjali', canBrew, canDeliver).brew('masala chai') gives 'Anjali brews masala chai', and she cannot bill. The ability objects must not change: canBrew still has only brew.

What you will learn

  • extends and super(...): a class built on another, and the call that runs the parent's constructor.
  • Overriding and super.method(): replace a parent's method while still using it.
  • Extending Error: an error with its own name and details.
  • Composition: combining small objects of methods instead.

More in The kitchen

Build "Inheritance" yourself

Tapri, a chai brand going online, needs its menu, orders and kitchen in code. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.