Loading video player…

Porting a Dataclass to Fields and a Constructor

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":

Language: Python
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:

Language: JavaScript
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:

Language: Program Output
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:

Language: JavaScript
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:

Language: Program Output
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:

Language: JavaScript
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:

Language: Program Output
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.

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.