1. In case the object operator
Conversion rule is:
1) first calls itself valueOf () method, raw data, return, or 2);
2) then calls itself toString () method, if not the original data, Uncaught TypeError;
There is one exception: Instead Date object, first call toString (), after a call valueOf ().
There obj = { valueOf(){ return 5; }, toString() { return 'hello'; } } 1 + obj; // 6; "1" + obj; // "15"
2. In case of the original data Operators
And transformation of:
1) If the value of the operator is required, calls valueOf ();
2) If the operator requires string, call toString ();
3) If either value may be a string, a priority call valueOf ();
// example of Boolean values ( to true ) .valueOf (); // . 1 ( to true ) .toString (); // "to true" 1+true; // 2 "1" + true; // 1true
3. String () function in case of tool objects
And transformation of:
1) first call toString (), to determine whether to return the original data; if it is, the end, or 2)
2) call valueOf ()
There obj = { valueOf(){ return 5; }, toString() { return 'hello'; } }; String(obj); // "hello"
4. Number () function in case of tool objects
And transformation of:
1) first call valueOf () method, if the return to the original data, ending; otherwise 2)
2) call toString () method
There obj = { valueOf(){ return 5; }, toString() { return 'hello'; } }; Number(obj); // 5