Following Ordinary Object Conversion
In this lesson, you ported the Reading class of Mina’s gallery display, whose screen shows the
label 21 C, from Python to JavaScript and followed how JavaScript turns the reading into text or a
number. In Python, str() and an f-string use __str__() and give 21 C, and float() uses
__float__() and gives 21.0. But reading + 1 raises a TypeError: Python’s arithmetic operators,
such as + and *, never fall back to __float__().
- In the port,
toString()takes over from__str__(), andvalueOf()from__float__():valueOf()returns a primitive that stands in for the object, here the number21. String(reading)and`${reading}`print21 C, andNumber(reading)prints21.reading + 1gives22andreading * 2gives42: JavaScript callsvalueOf()for you when the reading sits beside an arithmetic operator such as+or*.- Jonah’s label shortcut,
reading + "", prints21, not21 C. A log line in each method shows why:String()and the template literal runtoString(), whilereading + "",Number(),reading + 1andreading * 2runvalueOf(). - To get a primitive from an object, JavaScript runs an internal step the specification calls
ToPrimitive. It gets a hint:
stringfromString()and template literals triestoString()first;numberfromNumber()and*triesvalueOf()first;defaultfrom+acts likenumberfor ordinary objects. - Python’s
__add__()can redefine+for a class;valueOf()can’t. It only supplies the primitive that ordinary addition then uses. - A hint sets an order, not a single method. If the first method returns an object, JavaScript
tries the other one: a test object whose
valueOf()returns{}and whosetoString()returns"21"logsvalueOf, thentoString, andNumber()gives21. If both return objects, JavaScript throwsTypeError: Cannot convert object to primitive value. - The default
Object.prototype.valueOf()returns the object itself, so objects using that default fall through totoString(). - A Date is a built-in exception: its default hint acts like string, so
+joins its text.
Here’s Mina’s display line, converting on purpose, with the Python version first:
class Reading:
def __init__(self, celsius):
self.celsius = celsius
def __float__(self):
return float(self.celsius)
def __str__(self):
return f"{self.celsius} C"
reading = Reading(21)
next_celsius = float(reading) + 1
print(f"Reading: {reading}; next: {next_celsius:g} C")
The JavaScript port builds the label with a template literal and converts with Number() before
adding one:
class Reading {
constructor(celsius) {
this.celsius = celsius;
}
valueOf() {
return this.celsius;
}
toString() {
return `${this.celsius} C`;
}
}
const reading = new Reading(21);
const nextCelsius = Number(reading) + 1;
console.log(`Reading: ${reading}; next: ${nextCelsius} C`);
Python’s :g drops the point zero from 22.0, so both programs print the same line:
Reading: 21 C; next: 22 C
Convert at the boundary: String() or a template literal for text, Number() for numbers, and
don’t let + pick a method for you. A later lesson shows how a class can answer each hint itself.
Next up is the text lesson Reading the Spec’s Conversion Steps. There, you’ll trace the steps that turn missing readings and arrays of notes into strings, numbers, and Booleans.