I want an object to return one value in a numeric context and a completely different one in a string context. The following doesn't work.
foo = {
toString: function() { return "string" },
valueOf: function() { return 123 }
}
console.log(foo * 2) // got 246, fine
console.log("hi " + foo) // got "hi 123", want "hi string"
The addition operator will convert both operands to primitives using the internal abstract operation ToPrimitive
, and then, if one operand is a string, it will use the internal abstract operation ToString
to convert both to strings (note: this is different from the toString
method on userland objects.)
The upshot is, that with addition of Symbol.toPrimitive
to the language, you can now achieve your goal:
const foo = {
[Symbol.toPrimitive](hint) {
switch (hint) {
case "string":
case "default":
return "string"
case "number":
return 123
default:
throw "invalid hint"
}
}
}
console.log(foo * 2) // 246
console.log("hi " + foo) // "hi string"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With