One, the + operator procedure for reference type objects (I mean String,date,object,array,function,boolean) is as follows!
1, first call the ValueOf method of this object, get the return value a
2, and then convert this number a to numbers and get the final value.
My test is as follows:
Copy Code code as follows:
function W (s) {
Document.writeln ("<br/>");
Document.writeln (s);
Document.writeln ("<br/>-----------------------------");
}
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
Second, for basic data data types, the values are converted to numbers
Copy Code code as follows:
W (+5);//Output 5
W (+true);//Output 1
W (+false);//Output 0
W (+ "ss");//Output Nan
W (+ "111");//Output 111