Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Porting the Pool Passes Module

Assemble Swimmer, Pass, TenSwimCard and Membership using explicit constructors, per-instance fields, private state, static fields and a factory. Call super(swimmer) before this in each subclass, and use super.checkIn() in the card’s override.

Print each pass through a template literal so toString() supplies its line. Read the price through the class name. Run node --check pool-passes.js, then node pool-passes.js. This video’s first part prints four lines:

Language: Program Output
Priya Nair (adult): ten-swim card, 3 left
Tom Hale (concession): membership, 6 months
Grace Obi (junior): ten-swim card, 10 left
Ten-swim card: £4.50 a swim

Finish the Pool Passes Module adds the Early Lanes bookings and Tom’s renewal preview on a copy. That exercise produces the complete nine-line result while the membership on file stays at six months.

00:00 Riverside Pool’s new online booking is written in JavaScript, so the passes module moves too. It has a Swimmer dataclass, a Pass with a check-in and a printable line, and two kinds of pass: the ten-swim card and the membership.

00:17 Run it, and it prints four lines.

00:21 You’ll rebuild it in JavaScript until the port prints the same four. The dataclass writes Swimmer’s __init__() for you. In JavaScript, you write the constructor, with "adult" as the group’s default.

00:36 Bare fields alone would leave the name undefined, as you saw earlier, so each value is assigned to this.

00:44 Pass declares swims as a field, starting at zero. Its constructor takes only the swimmer: there’s no self in the list, because this arrives with the call.

00:56 describe() returns "casual swim", and checkIn() adds one to swims, then builds its line in a template literal.

01:05 Python’s __str__() becomes toString(). It joins the swimmer’s name, the group in parentheses and the describe() text.

01:14 JavaScript calls it whenever a pass goes into a template literal. The ten-swim card extends Pass. The price and the swim count belong to the class, so they’re static fields.

01:28 The swims left sit in a private field, and a getter lets the booking page read them, where Python uses a property. describe() shows them too.

01:39 The constructor defaults the swims left through the class name, since a static is a property of the class, never a plain name. Python works out the default while the class body runs, where SWIMS is a name.

01:53 Then super() comes first. Python sets its underscore attribute first, but in JavaScript, this doesn’t exist until super() has run, so the private field is set after it.

02:07 The card’s checkIn() overrides Pass’s. An empty card throws a RangeError, where Python raises a ValueError.

02:15 Otherwise, -= takes one swim off, the way += adds one, and super.checkIn() returns Pass’s line.

02:25 costPerSwim() reads the price through the class name too. Python’s self.PRICE finds the class attribute, but on a JavaScript card it’s undefined, and the cost is NaN.

02:38 The class method becomes static fromRecord(), which builds a card from a saved record. The record’s key is camelCase in JavaScript, read with a dot, where the dict needs brackets.

02:51 Membership extends Pass the same way. super() comes first, then months is set. Its describe() gives the months, and the rest comes from Pass.

03:03 At the bottom, JSON.parse() turns the saved record into an object. Priya’s card starts with three swims, Tom’s membership with six months, and Grace’s card comes from the record.

03:17 The loop puts each pass in a template literal: console.log() on its own shows the fields, not the line. The cost line follows, with two decimals.

03:30 node --check reads the file without running it. It prints nothing: the syntax is fine. Now run it. Priya’s line comes first. Membership has no toString() of its own.

03:47 Whose describe() does Pass’s toString() call for Tom? Your turn: pause the video and predict.

03:58 The membership’s.

04:02 Inside toString(), this is Tom’s pass, so his line ends with membership, six months. Grace’s card shows ten left, and the cost is £4.50.

04:15 Run the Python, and its four lines match the port’s exactly. That’s the module, the JavaScript way: no self, super() first, statics through the class name, and toString() for printing.

04:32 Next up, the exercise Finish the Pool Passes Module. You’ll book the three passes onto Early Lanes, whose two lanes leave Grace on the waitlist. Then you’ll preview Tom’s twelve-month renewal on a copy, while his membership stays at six, until the port prints the full module’s nine lines.

Become a Member to join the conversation.