Is there any expression where by an object's toString method is implicitly called overriding its valueOf method?
In the examples below, valueOf is always called implicitly (overriding toString).
"4" + {
toString: function () {
return "4";
},
valueOf: function () {
return 6;
}
}; // => "46", was expecting "44"
4 + {
toString: function () {
return "6";
},
valueOf: function () {
return 4;
}
}; // => 8
4 + {
toString: function () {
return 6;
},
valueOf: function() {
return "4";
}
}; // => "44"
i.e.:
Can we write an expression where by toString is implicitly called over valueOf (i.e without explicitly calling toString)?
If the hint is String , then toString is used before valueOf . But, if the hint is Number , then valueOf will be used first. Note that if only one is present, or it returns a non-primitive, it will usually call the other as the second choice.
toString() ). This method is inherited by every object descended from Object , but can be overridden by descendant objects (for example, Number. prototype. toString() ).
So just because a data class defines a toString doesn't mean you shouldn't override it.
So, whenever you use or print a reference variable of type in which toString() method is not overrided, you will get an output like above. You will not get what the object actually has in it. There will be no information about state or properties of an object.
Is there any expression where by an object's toString method is implicitly called overriding its valueOf method?
Yes, that does happen whenever the abstract ToString
operation is applied on an object, using the DefaultValue
procedure with .toString()
before .valueOf()
.
However, in your examples you have only used the addition operator which is like an exception from the standard behaviour. Since it does not only concatenate strings, but also sums numbers, it always uses valueOf
on both operands before checking whether they're strings. See the note 1:
No hint is provided in the calls to ToPrimitive in steps 5 and 6. All native ECMAScript objects except Date objects handle the absence of a hint as if the hint Number were given; Date objects handle the absence of a hint as if the hint String were given. Host objects may handle the absence of a hint in some other manner.
So which operations do implicitly use the ToString
instead of a hint-less ToPrimitive
? Here are a few examples:
in
operator, bracket notation, Object.getOwnPropertyDescriptor
, Object.defineProperty
, hasOwnProperty
, …parseInt
, parseFloat
, encodeURI[Component]
, decodeURI[Component]
, [un]escape
, Array::join
, String::[last]indexOf
, RegExp::exec
Function
constructorArray::sort
algorithm's default compare functionString
constructor and String methods when casting the this
object to a stringRegExp
constructorError
constructorsalert
, XMlHTTPRequest::open
, querySelector
, …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