Riverside Pool’s online booking needs a JavaScript Pass class that remembers a holder and counts check-ins. You’ll port the Python class before building the pool’s other passes on it. How does the new instance reach its methods?
Port the Constructor
Python’s Pass.__init__(self, holder) sets self.holder and self.swims = 0. Its check_in(self) increments the count and returns an f-string. The class keyword, PascalCase name, and methods inside the body carry over.
Braces mark bodies because JavaScript doesn’t read indentation. Class methods use their name, parameters, and body, without def or function. Watch out for the object literal resemblance: commas between methods give SyntaxError: Unexpected token ','.
JavaScript requires new Pass("Priya Nair") where Python uses Pass("Priya Nair"). Calling the JavaScript class without new gives TypeError: Class constructor Pass cannot be invoked without 'new'.
A line-by-line port can still leave Python’s instance parameter in place. Predict what self holds when this constructor runs:
class Pass {
constructor(self, holder) {
self.holder = holder;
self.swims = 0;
}
}
const priya = new Pass("Priya Nair");
JavaScript stops with this error line:
TypeError: Cannot create property 'holder' on string 'Priya Nair'
JavaScript never passes the instance as an argument, so "Priya Nair" fills self and holder is undefined. Likewise, calling checkIn(self) without arguments makes self undefined, giving TypeError: Cannot read properties of undefined (reading 'swims').
Use this for the Instance
Drop the instance parameter and write this, JavaScript’s keyword for the receiver. In the constructor, it’s the new instance created and returned by new. In an ordinary method call such as priya.checkIn(), it’s the object before the dot. That matches Python’s self for these calls. Passing methods as callbacks has different rules, covered later.
With the instance parameter removed, the syntax changes like this:
| Python | JavaScript |
|---|---|
class Pass: |
class Pass { ... } |
def __init__(self, holder): |
constructor(holder) { ... } |
def check_in(self): |
checkIn() { ... } |
self.holder |
this.holder |
Pass("Priya Nair") |
new Pass("Priya Nair") |
Use camelCase for the method and a template literal, JavaScript’s backtick string with ${...} substitutions, for its message:
class Pass {
constructor(holder) {
this.holder = holder;
this.swims = 0;
}
checkIn() {
this.swims += 1;
return `${this.holder} checked in, swim ${this.swims}`;
}
}
const priya = new Pass("Priya Nair");
const tom = new Pass("Tom Hale");
console.log(priya.checkIn());
console.log(tom.checkIn());
Each pass reaches swim one independently, and console.log() displays the results:
Priya Nair checked in, swim 1
Tom Hale checked in, swim 1
Using the same Pass class, inspect the port: What does logging an instance show, does each instance get a separate method function, and what type does JavaScript report for the class? Run these checks:
class Pass {
constructor(holder) {
this.holder = holder;
this.swims = 0;
}
checkIn() {
this.swims += 1;
return `${this.holder} checked in, swim ${this.swims}`;
}
}
const priya = new Pass("Priya Nair");
const tom = new Pass("Tom Hale");
priya.checkIn();
console.log(priya);
console.log(priya.checkIn === tom.checkIn);
console.log(typeof Pass);
The console prints the instance, the method comparison, and the class’s type:
Pass { holder: 'Priya Nair', swims: 1 }
true
function
Node’s console shows the class name and current properties, whereas Python’s default print(priya) shows an object address.
The true result means both instances share one method function: it gets the receiver from priya.checkIn() or tom.checkIn(), so each call still updates its own pass. In Python, looking up priya.check_in gives a bound method carrying priya. In JavaScript, priya.checkIn gives the shared function without attaching priya.
The last check reports "function" for the class itself. You’ll examine the underlying machinery later.
Keep this out of parameter lists and use new to create instances.
In the next lesson, Porting a Dataclass to Fields and a Constructor, you’ll port the module’s Swimmer dataclass with JavaScript fields and an explicit constructor.