SOURCE

function Value(val){
    this.val = val;
}

Value.prototype.valueOf = function(){
    console.log('trigger valueOf');
    return this.val;
}

Value.prototype.toString = function(){
    console.log('trigger toString');
    return `str ${this.val}`;
}

Value.prototype.toNumber = function(){
    console.log('trigger toNumber');
    return Number(this.val);
}

Value.prototype.toBoolean = function(){
    console.log('trigger toBoolean');
    return Boolean(this.val);
}



// Value.prototype[Symbol.toPrimitive] = function(hint){
//     console.log('trigger Symbol.toPrimitive', hint);
//     return Boolean(this.val);
// }


var a = new Value(1);

//  trigger valueOf
// console.log( ++ a);
console.log( +a ) 
console.log( -a )
console.log( 1 + a );
console.log( a + 3 );
console.log( '123' + a  )  

 // trigger toString
console.log(`${a}`) 


console.log( !!a ); // ?





console 命令行工具 X clear

                    
>
console