Loading video player…

Porting a Dataclass to Fields and a Constructor

A JavaScript field gives each instance an initial value. It doesn’t turn arguments into properties: write a constructor to accept name and group = "adult", then assign them to this.

Keep swims = 0; in the class body when every pass starts with zero swims. A field such as log = []; creates a new array for each instance, unlike a Python list stored as a class attribute. JavaScript’s console view doesn’t supply a dataclass’s equality behavior.

Next, fix the constructor, fields and methods in Port the Lane Bookings.

00:00 With Pass ported, your next port is Swimmer, and in Python it’s a dataclass. It has two fields: a name, and a group that defaults to "adult".

00:11 The @dataclass decorator writes the __init__() method for you, with the fields as its parameters in order, so Tom Hale prints with his concession group.

00:19 Now port it to JavaScript.

00:23 JavaScript has no dataclass, but a class body can declare fields. A field is a name, with or without a starting value, ended by a semicolon, like a statement.

00:36 A line-by-line port writes the fields as the dataclass lists them, and the type hints drop. There’s no decorator to import. Tom’s line ports with new, as Pass’s did, and the last line logs him.

00:52 Run the port, and JavaScript logs one line for Tom. What does that line show? Your turn: pause the video and predict.

01:07 A Swimmer with no name. His name is undefined, JavaScript’s value for nothing set, and his group is still 'adult'.

01:17 There’s no error, and both of his arguments are gone.

01:22 A field only says what every instance starts with. name has no starting value, so it starts as undefined, and group as "adult".

01:33 Neither says where an argument goes. In Python, the decorator generated an __init__() method that takes the fields as parameters. Without the decorator, Python refuses Tom’s arguments with a TypeError.

01:48 JavaScript has nothing to write that method, so this class gets an empty constructor. Nothing takes the arguments, and JavaScript drops extra ones without a word.

02:00 So write the constructor yourself. It takes name and group, and group gets a default, "adult", as a function’s parameter would.

02:10 Its body assigns each one to this, as Pass’s constructor did. The constructor sets both now, so the field lines aren’t needed. Run it again, and Tom is a Swimmer with his name and his concession group.

02:27 Priya is an adult, so her line leaves the group out. Log her, and the default fills in: '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.

02:48 The class didn’t write that: the console lays out any object this way. Giving a class its own printed form, and the equality method the dataclass wrote, come later in the course.

03:01 Fields do have a job: state that every instance starts with. The module’s Pass now takes a swimmer, and its __init__() method sets the swim count to 0.

03:13 In the port, the swimmer comes in through the constructor, because the caller passes it. The count moves up into the class body as a field, swims = 0;, because every pass starts there.

03:26 The checkIn() method ports as before.

03:30 Run it, and Tom checks in for swim 1. Log the pass, and swims comes first: fields are set before the constructor’s body runs. Each new pass runs the field’s line again.

03:45 Watch out, the same line means two things: in a Python class body, log = [] is one list every instance shares, but a field runs for each new pass, so each pass gets its own array.

04:00 In JavaScript, nothing turns fields into constructor parameters for you. What the caller passes goes in its parameters, with their defaults, and what every instance starts with goes in fields.

04:13 A one-off record that only holds data is often an object literal, much like a dict. Keep a class when every instance is built the same way, like Swimmer, or has methods, like Pass.

04:27 Next up, Port the Lane Bookings: a teammate’s booking board should print the same lines as the Python for Luis and Hana.

Become a Member to join the conversation.