JavaScript Classes for Pythonistas (Summary)

Congratulations, you made it to the end of the course! Riverside Pool’s passes module now runs in JavaScript and prints the same nine lines as pool_passes.py. The output runs from Priya Nair (adult): ten-swim card, 3 left to On file: Tom Hale (concession): membership, 6 months, with Grace Obi on the Early Lanes waitlist in between.

In Porting the Pool Passes Module, you ported the four classes and the module’s first four lines. In the exercise Finish the Pool Passes Module, you completed it, booking the three passes onto Early Lanes and previewing Tom’s renewal on a copy. Each Python class kept its job and became what a JavaScript developer would write.

In this course, you learned how to:

  • Port a class to JavaScript with a constructor that sets properties on this, methods that never list the instance as a parameter, and new at every call that makes an instance
  • Replace a dataclass with fields in the class body and a constructor that takes the arguments, as Swimmer does with constructor(name, group = "adult")
  • Keep state in a # private field, read it through a get getter without parentheses, and check every new value in a set setter
  • Share a price and a factory with static, as in TenSwimCard.PRICE and TenSwimCard.fromRecord()
  • Extend a class with extends, call super(swimmer) before any use of this, and reach the parent’s method with super.checkIn()
  • Print instances with a toString() method and template literals
  • Copy and save instances through their own methods, a .copy() that calls new and a .toJSON() that returns the record
  • Pass a method as a callback inside an arrow function, (name) => waitlist.add(name), so this stays the waitlist

Keep the Habits That Carry Over

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

Become a Member to join the conversation.