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

Extending Pass With extends and super

class TenSwimCard extends Pass inherits the pass’s methods. In the subclass constructor, call super(swimmer) before using this; then initialize the card’s own state.

An override calls the parent’s version with super.checkIn(), without the Python-style parentheses after super. The receiver stays the card, so a call to this.describe() inside the parent reaches the card’s override. A card is both instanceof TenSwimCard and instanceof Pass.

Next, use Printing Instances With toString() to give passes their printable lines.

00:00 Riverside Pool sells three kinds of pass, and the Python module makes the ten-swim card a kind of pass. Your JavaScript card is still a class of its own. In Python, Pass now has a describe() method, the part each kind of pass fills in, and check_in() builds its line from it.

00:19 Run it, and Luis Ortega checks in for a casual swim. You’ll port that, then make the card a subclass.

00:28 In JavaScript, Pass gains the same method. checkIn() now calls this.describe() for its line, as Python’s does. Run the port, and Luis gets the same line.

00:42 For the card, Python names its parent in parentheses; JavaScript uses the keyword extends. The count stays in a private field; the card keeps only what this lesson needs.

00:56 Port the __init__() method line by line. The module’s card sets its own count first, then hands the swimmer to Pass’s __init__() through super().

01:06 In a constructor, JavaScript’s super(), with the swimmer, runs Pass’s constructor. The describe() method ports with a template literal.

01:18 Before porting checkIn(), try Priya’s card, with three swims, and log its description. Python prints ten-swim card, three left. What does the JavaScript port log?

01:31 Your turn: pause the video and predict.

01:39 A ReferenceError, and nothing is logged. A derived class, meaning a subclass, must call the super constructor before it uses this, the new object.

01:51 The trace points at the constructor’s first line. In Python, the object exists before __init__() starts. Python passes it in as self, so __init__() can set the swims left before calling the parent’s.

02:08 A JavaScript subclass constructor starts with no object at all, so setting #swimsLeft first fails. In a class with no parent, like Pass, new makes the object first.

02:21 In a subclass, the parent’s constructor creates this, so there’s no this until super() has run. Pass’s constructor makes the object; only then does the card add its count.

02:36 So the fix: call super() before any use of this, in practice the constructor’s first line, then set the card’s own field. Run it again, and the port logs ten-swim card, three left.

02:50 That’s how JavaScript developers write it: super() first, then the subclass’s own fields. Python’s order is fine in Python; if yours calls super() first, it ports as it is.

03:04 The card’s checkIn() overrides Pass’s, as in Python. The empty-card check and the -= port as you’d expect, with a RangeError for Python’s ValueError.

03:15 The new part: super.checkIn() calls Pass’s version, and this is still the card.

03:23 Watch out for Python’s parentheses: super(), with parentheses, in a method stops the file with a SyntaxError before anything runs.

03:32 super.checkIn() reaches the parent’s version directly, so super takes no parentheses.

03:40 Now check Priya in. The card’s checkIn() takes one of her three swims, then calls Pass’s version. That calls this.describe(), and this is still Priya’s card, so the card’s describe() runs.

03:54 Both languages print Priya Nair checked in, ten-swim card, two left. Overriding works as in Python.

04:04 Hana Sato’s card is empty, so her checkIn() throws a RangeError.

04:10 Ask whether Priya’s card is a TenSwimCard and a Pass: instanceof says true twice, as isinstance() does in Python.

04:19 But extends takes one class, so there’s no multiple inheritance; other patterns for that come later. How a card finds Pass’s methods is a later topic too; for now, it works as in Python.

04:34 The JavaScript way: in a subclass constructor, super() comes first, then the subclass’s own fields. In a method, super.name() calls the parent’s version.

04:47 Keep hierarchies shallow, as in Python: one level, Pass and its kinds of pass.

04:55 Next up is Printing Instances With toString(). You’ll port Python’s __str__ to a toString() method that builds Pass’s printable line.

Become a Member to join the conversation.