1. The operator operation process for reference type objects (I refer to String, Date, Object, Array, Function, Boolean) is as follows!
1. First call the valueOf method of this object to get the return value A
2. Then convert this value A into a number and get the final value
My test is as follows:
function w(s){
document.writeln("
");
document.writeln(s);
document.writeln("
--------------------------------");
}
String.prototype.valueOf=function(){return 1;};
w( new String("sss"));//Output 1
String.prototype.valueOf=function(){return "a ";};
w( new String("sss"));//Output NaN
Date.prototype.valueOf=function(){return 1;};
w ( new Date());//Output 1
Date.prototype.valueOf=function(){return "a";};
w( new Date());//Output NaN
Object.prototype.valueOf=function(){return 1;};
w( {});//Output 1
Object.prototype.valueOf=function(){return "a";};
w( {});//Output NaN
Array.prototype.valueOf=function(){return 1;};
w( []);//Output 1
Array.prototype.valueOf=function(){return "a";};
w( []);//Output NaN
var s=function(){};
Function. prototype.valueOf=function(){return 1;};
w( s);//Output 1
Function.prototype.valueOf=function(){return "a";};
w( s);//Output NaN
Boolean.prototype.valueOf=function(){return 1;};
w( new Boolean());//Output 1
Boolean.prototype. valueOf=function(){return "a";};
w( new Boolean());//Output NaN
Two, for basic data data types, the value is converted into a number
w( 5);//output 5
w( true);//Output 1
w( false); // Output 0
w( "ss"); // Output NaN
w( "111"); // Output 111