Previous words
For type conversions, the two common methods for objects are ToString () and valueof (). In fact, these two methods can also be applied to the wrapper type. The ToString () method, described earlier in this article, describes the valueof () method, which returns the original value
"1" Undefined and null No valueof () method
Undefined.valueof (); // Error null. valueOf (); // Error
"2" Boolean data True and False returns the original value
true. ValueOf (); // true typeof true. ValueOf (); // ' Boolean ' false. ValueOf (); // false typeof false. ValueOf (); // ' Boolean ' Boolean.valueof (); // Boolean () {[native code]} typeof Boolean.valueof (); // ' function '
"3" String type original value returned
' 1 '. valueOf (); // ' 1 ' ". ValueOf (); // "' ' ABC '. valueOf (); // ' abc ' String.valueof (); // String () {[native code]} typeof String.valueof (); // ' function '
"4" Numeric types are divided into integers and floating-point numbers for processing
Number.valueof (); // Number () {[native code]} typeof Number.valueof (); // ' function '
1, the integer directly follow. ValueOf () Form, will error, prompt invalid mark, so try to add parentheses
0. valueOf (); // uncaught syntaxerror:invalid or unexpected token (0). ValueOf (); // 0+0. valueOf (); // uncaught syntaxerror:invalid or unexpected token (+0). ValueOf (); // 0-0. valueOf (); // uncaught syntaxerror:invalid or unexpected token (-0). ValueOf (); // -0
[Note that the valueof () value of]-0 is-0, while the ToString () value of-0 is ' 0 '
2, floating-point value of the original return
1.23. valueOf (); // 1.23+1.23. valueOf (); // 1.23-1.23. valueOf (); // -1.23 Nan.valueof (); // NaN Infinity.valueof (); // Infinity-infinity.valueof (); // -infinity
[Note] Unlike ToString (), valueOf () is not allowed to receive the conversion cardinality
"5" Object type and custom object type return the original object
{}.valueof (); // error, unexpected token. ({}). ValueOf (); // object{} typeof ({}). ValueOf (); // ' object ' ({a:123 }). ValueOf (); // object{a:123} Object.valueof (); // object () {[native code]} typeof Object.valueof (); //
function Person () { this'test';} var New Person ();p erson1.valueof (); // Person {name: ' Test '}
"6" Function type returns the original function
function Test () { alert (1); Test}test.valueof (); /* function Test () { alert (1);//test }*/function.valueof (); // Function () {[native code]}
"7" Array type returns the original array
[].valueof (); // [ ][1].valueof (); // [1] [1,2,3,4].valueof (); // [1,2,3,4] Array.valueof (); // Array () {[native code]}
"8" is different from other objects, the time date type returns a numeric value, which is the this time value
Date.now (); // 1465115123742 (new Date ()). ValueOf (); // 1465115123742 typeof (new Date ()). ValueOf (); // ' number ' Date.valueof (); // Date () {[native code]}
"9" Regular expression regexp type returns the original regular object
/ab/i.valueof (); /// ab/i/mom (and Dad (and baby)?)? /gi.valueof (); // Mom (and Dad (and baby)?)? /giregexp.valueof (); // RegExp () {[native code]}
"10" Error type
Error.valueof (); // Error () {[native code]} Rangeerror.valueof (); // Rangeerror () {[native code]} Referenceerror.valueof (); // Referenceerror () {[native code]} Syntaxerror.valueof (); // SyntaxError () {[native code]} Typeerror.valueof (); // TypeError () {[native code]} Urierror.valueof (); // Urierror () {[native code]}
Summarize
1. The main difference between ToString () and valueof () is that ToString () returns a string, whereas valueof () returns the original object
2, because undefined and null are not objects, their ToString () and valueof () two methods are not
3. The ToString () method of the numeric number type can receive the conversion cardinality, returning a numeric value in the form of a different binary string, while the valueof () method cannot accept the conversion cardinality
4. The time date type of the ToString () method returns a string representation of the time, whereas the valueof () method returns the number of milliseconds for a numeric type that is now 00:00:00 to January 1, 1970.
ValueOf () method