将对象f进行字符串转换,但不调用原型链的toString方法
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'
尝试
console.log(`before ${f} after`);
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'