What is happening between for example alert(+user); and its method deciding that hint is 'number'? So far I can't explain it other than "oh they tried to converse it to a number, so let's do that". I would like to know how that actually looks like.
let user = {
name: "John",
money: 1000,
[Symbol.toPrimitive](hint) {
alert(`hint: ${hint}`);
return hint == "string" ? `{name: "${this.name}"}` : this.money;
}
};
// conversions demo:
alert(user); // hint: string -> {name: "John"}
alert(+user); // hint: number -> 1000
alert(user + 500); // hint: default -> 1500