JSFiddle - React, Tailwind, and code Playground
by Khalil Zhang
JavaScript
var hello = {
toString: function() {
return "hello";
},
valueOf: function() {
return 1;
}
};
//call toString
document.write(hello + '<br/>'); //'hello'
//call valueOf
document.write(hello + 1 + '<br/>'); //2
//call valueOf
document.write(hello + 'world' + '<br/>'); //'1world'
//firstly, call Object.prototype.valueOf(not primitive value),
//and then call toString
delete hello.valueOf;
document.write(hello + 1 + '<br/>'); //'hello1'
//firstly, call Object.prototype.valueOf(not primitive value),
//and then call Object.prototype.toString
delete hello.toString;
document.write(hello + 1 + '<br/>'); //'[object Object]1'
//Object.prototype.toString is not object, so throw exception
delete Object.prototype.toString;
document.write(hello + 1 + '<br/>'); //throw TypeError exception