Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Porting the Gallery Formatter

Porting the Gallery Formatter

You ported Mina’s Python reading formatter to JavaScript: the Reading class, a subclass with a conversion hook, and one formatted line for the 21-degree reading. The port keeps the number and the label apart, and converts on purpose.

  • constructor does __init__’s job, and this stands in for self. __str__ becomes toString(), with a template literal in place of the F string.
  • __float__ becomes valueOf(). Python’s arithmetic operators never call __float__, so Reading(21) * 2 raises a TypeError. JavaScript calls valueOf() by itself whenever an operator needs a number, so a plain reading of 21 times 2 gives 42.
  • Plain Reading keeps ordinary conversion. The subclass LoggedReading decides each hint on purpose: it extends Reading and adds one method whose computed key is the predefined symbol Symbol.toPrimitive, not a string. JavaScript calls it first and passes a hint.
  • For "string", the hook returns the label. For "number" and "default", it returns the plain number. Plus passes "default", so a hook that returned the label there would make reading + 1 glue 21 C1.
  • In Python, __str__ covers the string hint and __float__ the number hint, with no direct equivalent of default.
  • With the hook present, JavaScript uses it and doesn’t call the inherited toString() or valueOf(). String(reading) and `${reading}` give "21 C", reading + 1 gives 22, and reading * 2 gives 42. A plain Reading gives the same four values the ordinary way: the hook doesn’t change them, it puts each decision in one place.
  • Python’s float() becomes Number(), which passes the number hint. In Python, adding one to the reading itself raises a TypeError, so float() had to be there. Plus would also give 22 here, through default, but Number() keeps the conversion on purpose.

Here’s the port, gallery.js:

Language: JavaScript
class Reading {
  constructor(celsius) {
    this.celsius = celsius;
  }

  toString() {
    return `${this.celsius} C`;
  }

  valueOf() {
    return this.celsius;
  }
}

class LoggedReading extends Reading {
  [Symbol.toPrimitive](hint) {
    if (hint === "string") return `${this.celsius} C`;
    return this.celsius;
  }
}

const reading = new LoggedReading(21);

const nextCelsius = Number(reading) + 1;
console.log(`Reading: ${reading}; next: ${nextCelsius} C`);

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

00:00 You’ll port the gallery’s reading formatter to JavaScript: the class first, then one formatted line. Mina’s Python script keeps each reading’s number and label apart. __str__ gives the label, and __float__ gives the number.

00:17 Each line converts on purpose. The f-string asks for the label, and float() asks for the number before adding one. In JavaScript, formatting and arithmetic each convert an object their own way, so the port spells out every conversion.

00:34 Reading becomes a JavaScript class. Its constructor does the job of __init__, and this stands in for self, with no parameter of its own.

00:44 __str__ becomes toString(), with a template literal in place of the f-string: backticks, and the value inside a dollar sign and braces.

00:55 __float__ becomes valueOf(), returning the number. Here the analogy stops. Python’s arithmetic operators never call __float__: a reading times two raises a TypeError.

01:08 JavaScript calls valueOf() by itself whenever an operator needs a number, so a plain reading times two gives 42.

01:18 Plain Reading keeps ordinary conversion, and a subclass decides each hint on purpose. LoggedReading extends Reading, as class LoggedReading(Reading) would in Python.

01:30 Its one method has a computed name in square brackets: the key is the predefined symbol Symbol.toPrimitive, not a string. JavaScript calls this hook first whenever it needs a primitive from the object.

01:45 It passes a hint saying what kind of value it wants. The if uses === to compare the hint strictly, and with one statement it needs no braces.

01:58 For "string", the hook returns the label. Otherwise it returns the plain number, for "number" and "default" alike.

02:06 Default matters, because the plus operator passes it. Return the label there, and reading + 1 glues 21 C1 instead of adding. In Python, __str__ covers the string hint and __float__ the number hint, but there’s no direct equivalent of default.

02:27 Test each hint on purpose, with four checks. LoggedReading still inherits toString() and valueOf(), but JavaScript calls the hook instead.

02:37 String() and the template literal ask for a string, plus passes default, and times asks for a number. The checks give 21 C twice, then 22 and 42.

02:50 A plain Reading gives the same four values. The hook doesn’t change them: it puts each decision in one place.

02:59 Now the line, for the first reading, 21 degrees. new creates it, where Python calls the class, and const gives it a name that can’t be reassigned.

03:09 Python’s float() becomes Number(), which passes the number hint, and the port adds one. In Python, adding one to the reading raises a TypeError, so float() had to be there.

03:23 Plus also adds one, through default, but Number() keeps the conversion on purpose. console.log() prints the line, as print() does.

03:34 You run the port with the node command, as you’d run the script with Python. Python’s float() plus one gives 22.0; its :g drops the decimal point.

03:46 The port’s line has no format spec. Your turn: pause the video and predict whether it prints 22 or 22.0.

03:59 It prints 22, with no decimal point.

04:03 JavaScript uses Number for both integers and fractions here, and whole numbers print without a decimal point. That’s Python’s first line, character for character.

04:15 The second line comes when the port handles the whole list. Keep the number and the label apart, and convert on purpose. For a measurement, give default the number, give string the label, and test each hint.

04:31 At an arithmetic boundary, call Number() even when an operator would convert for you. Next up is the exercise Finish the Gallery Formatter. You’ll format every reading in a fresh batch from the gallery, and turn away a missing reading before converting it.

Become a Member to join the conversation.