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

Porting the Pool Passes Module

Riverside Pool’s new online booking is written in JavaScript, so the Python 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. When you run it, it 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

In this lesson, you rebuilt it in JavaScript, class by class:

  • The dataclass writes Swimmer’s __init__() for you. In JavaScript, you write the constructor, with "adult" as the group’s default, and assign each value to this.
  • Pass declares swims = 0 as a field. Its constructor takes only the swimmer: there’s no self in the list, because this arrives with the call.
  • Python’s __str__() becomes toString(), which JavaScript calls whenever a pass goes into a template literal. console.log() on its own shows the fields, not the line.
  • TenSwimCard keeps its price and swim count in static fields, and its swims left in a private #swimsLeft field behind a getter. Its 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.
  • super(swimmer) comes first in both subclasses, because this doesn’t exist until it has run, so #swimsLeft and months are set after it.
  • The card’s checkIn() throws a RangeError on an empty card, where Python raises a ValueError; otherwise -= takes a swim off and super.checkIn() returns Pass’s line.
  • costPerSwim() reads TenSwimCard.PRICE: on a card, this.PRICE is undefined. The class method becomes a static method, fromRecord(), which reads the record’s camelCase key with a dot, where the dict needs brackets.

Locked learning resources

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

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

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

Unlock This Lesson

Already a member? Sign-In

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.