Handling All Three Conversion Hints
In this lesson, you give Mina’s class a conversion hook with a deliberate answer for each hint
that JavaScript can send. Her display uses a reading three ways: in a template literal, doubled, and plus 1 for the
next reading. With the ordinary valueOf() and toString() methods, the display shows 21 C,
42 and 22. The JavaScript lines hand the object itself to + and *. Python’s Reading
defines no __add__, so the Python version calls float() first.
In Python, the Reading class answers with two methods, __float__ for the number and __str__
for the text. The hook from the last lesson handles every string and number conversion in one place, so Mina
replaces valueOf() and toString() with one whose branches play both parts: the "number" hint returns the Celsius value, and every other hint
falls through to the label. An if without braces runs only the one statement after it, like
Python’s one-line if.
Run with node display.js, the template literal shows 21 C and doubling gives 42, but
reading + 1 logs 21 C1.
Logging the hint inside the hook shows why. A template literal asks for a "string" hint, *
asks for "number", and + sends "default", as in the ordinary steps. The port’s test lets
that hint fall through to the label. The ordinary steps treated "default" like "number", so
valueOf() went first and + added. The hook replaced those steps, so the default hint now lands
wherever the hook sends it.