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

Handling All Three Conversion Hints

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.

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 In this lesson, you’ll 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.

00:15 Each line prints with console.log(), like Python’s print() here. This class still has the ordinary valueOf() and toString() methods.

00:24 Run it, and the display shows 21 C, 42, and 22. The JavaScript lines hand the object itself to plus and times. Python’s Reading defines no __add__, so the Python version calls float() first.

00:42 In Python, the Reading class answers with two methods. __float__ gives the number, and __str__ gives the text. The hook from the last lesson handles every string and number conversion in one place, so Mina replaces valueOf() and toString() with it.

01:01 The "number" hint returns the Celsius value, as __float__ does. An if without braces runs only the one statement after it, like Python’s one-line if, so every other hint falls through to the label, the way __str__ answers for text.

01:18 Run the port with the node command, as before. The template literal shows the label, 21 C. Doubling gives 42, the Celsius value twice. What does reading + 1 log?

01:33 Your turn: pause the video and predict.

01:42 It logs 21 C1, as text.

01:46 The hook handed + the label, so + glued a 1 onto the string.

01:53 To see why, have the hook log the hint it receives, and run it again. The template literal asks for a "string" hint, as you saw for template literals before.

02:04 Doubling asks for a "number" hint. The + operator sends "default", as it did in the ordinary steps, and the port’s test lets it fall through to the label.

02:16 That’s where 21 C1 came from.

02:20 Why did that line change? The ordinary steps treated default like number, so valueOf() went first and plus added. The hook replaced those steps, so the default hint now lands wherever the hook sends it, and the port sends it to the label.

02:38 Python has no default hint. There, reading + 1 raises a TypeError unless the class defines __add__, which overloads the operator itself.

02:49 The JavaScript hook doesn’t overload anything: it returns a primitive, and + then does its usual work with that value.

02:58 So decide what default means for a reading. A temperature adds like a number, the same answer the ordinary steps gave, so only the request for text should get the label.

03:09 The fixed hook swaps its test: the "string" hint returns the label, and the "number" and "default" hints both get the Celsius value.

03:19 Run it again. The template literal still shows the label, and doubling still gives 42. Now reading + 1 gives 22. In a separate check, String() sends "string" too, so it gets the label.

03:36 Now that each hint gets the right answer, the log line goes. Now reading + 1 works, but it hides which conversion happens. Python’s version calls float() before the arithmetic.

03:49 In JavaScript, Number() asks for the "number" hint on purpose. So the display converts once, at the arithmetic, and keeps the label in the template literal.

04:01 Python needs :g to drop the point zero that float() adds, while JavaScript doesn’t split int and float, so 22 prints as 22. Both programs print the same line.

04:15 Give each hint its answer on purpose: the label for the "string" hint, and the Celsius value for the "number" and "default" hints.

04:23 Then convert with Number() where the display does arithmetic.

04:29 Next up is the exercise Format the Evening Reading. There, a new reading of 16 should display as 16 C, with 17 next.

Become a Member to join the conversation.