我有这样的代码:
function Answer() {
this[Symbol.toPrimitive] = function(hint) {
console.log('convert to ' + hint);
if (hint == 'string') {
return 'the answer is 42';
} else if (hint == 'number') {
return 42;
} else {
return "default";
}
}
}
var a = new Answer();
console.log(10 + a);
console.log(+a);
console.log(''+a);
console.log(`${a}`)
+object
使用“数字”调用Symbol.toPrimitive但是当我调用时:
10+object
或''+object
在Chrome中使用“默认”调用它