const temperature = {
celsius: 22,
[Symbol.toPrimitive](hint) {
if (hint === 'number') {
return this.celsius;
}
if (hint === 'string') {
return `${this.celsius}°C`;
}
return this.celsius + 273.15;
}
};
console.log(`Temp: ${temperature}`);
console.log(temperature + 0);
console.log(temperature * 2);
console.log(+temperature);
Post #3086
3.03K
CHALLENGE
- ❤ 5
- 🔥 5
- 👍 2