the object f string conversion, but not call the prototype chain toString method
function fn(){}
fn.prototype.toString = function(){
console.log("toString call");
return {};
}
fn.prototype.valueOf = function(){
console.log("valueOf call");
return 100;
}
let f = new fn();
console.log("before "+f+" after");
// I hope:toString call --> valueOf call --> 'before 100 after'
// Result: valueOf call --> 'before 100 after'
Try
When using
+
(addition or string concatenation), thehint
parameter that passed to[Symbol.toPrimitive](hint)
isdefault
, which will callvalueOf()
by default.But when using template string, the
hint
would bestring
, sotoString()
will be called first.