Porting a Dataclass to Fields and a Constructor
With Pass ported, the booking module’s Swimmer is next. In Python, it’s a dataclass with two
fields, name and a group that defaults to "adult":
from dataclasses import dataclass
@dataclass
class Swimmer:
name: str
group: str = "adult"
tom = Swimmer("Tom Hale", "concession")
print(tom)
The @dataclass decorator writes __init__() for you, with the fields as its parameters in
order, so Tom prints as Swimmer(name='Tom Hale', group='concession').
JavaScript has no dataclass, but a class body can declare fields: a name, with or without a starting value, ended by a semicolon, like a statement. A line-by-line port writes the fields as the dataclass lists them and drops the type hints:
class Swimmer {
name;
group = "adult";
}
const tom = new Swimmer("Tom Hale", "concession");
console.log(tom);
It runs without an error, and Tom has lost both his arguments:
Swimmer { name: undefined, group: 'adult' }
A field only says what every instance starts with: name starts as undefined, JavaScript’s
value for nothing set, and group as "adult". Neither says where an argument goes.
In Python, it’s the decorator that turns the fields into __init__() parameters: take it off, and Python
refuses the call with TypeError: Swimmer() takes no arguments. JavaScript has nothing to write
that method, so the class gets an empty constructor: nothing takes the arguments, and JavaScript
drops extra ones without a word.
So write the constructor yourself. A default parameter works as it does in a function, and the
body assigns each value to this. The constructor sets both, so the field lines aren’t needed:
class Swimmer {
constructor(name, group = "adult") {
this.name = name;
this.group = group;
}
}
const tom = new Swimmer("Tom Hale", "concession");
console.log(tom);
const priya = new Swimmer("Priya Nair");
console.log(priya);
Priya’s line leaves the group out, and the default fills in:
Swimmer { name: 'Tom Hale', group: 'concession' }
Swimmer { name: 'Priya Nair', group: 'adult' }
Python prints the same two swimmers. The console shows the class and its properties much as a dataclass’s repr does, with braces and colons for parentheses and equals signs. The class didn’t write that: the console lays out any object this way. A class’s own printed form, and the equality method the dataclass also wrote, come later in the course.
Fields do have a job: state that every instance starts with. The module’s Pass now takes a
swimmer, and its __init__() sets the swim count to 0. In the port, the swimmer comes in
through the constructor, because the caller passes it, and the count moves up into the class body
as a field, because every pass starts at 0:
class Swimmer {
constructor(name, group = "adult") {
this.name = name;
this.group = group;
}
}
class Pass {
swims = 0;
constructor(swimmer) {
this.swimmer = swimmer;
}
checkIn() {
this.swims += 1;
const name = this.swimmer.name;
return `${name} checked in, swim ${this.swims}`;
}
}
const tom = new Swimmer("Tom Hale", "concession");
const tomPass = new Pass(tom);
console.log(tomPass.checkIn());
console.log(tomPass);
Tom checks in for swim 1, as in Python, and the log lists swims first, because fields are set
before the constructor’s body runs:
Tom Hale checked in, swim 1
Pass {
swims: 1,
swimmer: Swimmer { name: 'Tom Hale', group: 'concession' }
}
Each new pass runs the field’s line again. Watch out, the same line means two things: in a Python
class body, log = [] is one list every instance shares, but as a field, log = []; runs again
for each new pass, so each pass gets its own array.
In JavaScript, nothing turns fields into constructor parameters for you. What the caller passes goes in the constructor’s parameters, with their defaults, and state that every instance starts with goes in fields.
A one-off record that only holds data is often a plain object literal, such as
{ name: "Tom Hale", group: "concession" }, much like a dict. Keep a class when every instance
is built the same way, like Swimmer with its default group, or when it has methods, like
Pass.
Next up is Port the Lane Bookings. A teammate has ported the lane booking board, and your job is to make it print the same lines as the Python for Luis Ortega’s and Hana Sato’s bookings.