The previous words
Regarding type conversion, two common methods for objects are toString() and valueOf(). In fact, these two methods can also be applied to the packaging type. The toString() method has been introduced before. This article will introduce the valueOf() method, which returns the original value
【1】Undefined and null have no valueOf() method
undefined.valueOf();//Error null.valueOf();//Error
【2】Boolean data true and false return to the original value
true.valueOf();//truetypeof true.valueOf();//'boolean'false.valueOf();//'falsetypeof false.valueOf();//'boolean'Boolean.valueOf();//Boolean() { [native code] }typeof Boolean.valueOf();//'function'【3】The original value of the string type is returned
'1'.valueOf();//'1'''.valueOf();//'''abc'.valueOf();//'abc'String.valueOf();//String() { [native code] }typeof String.valueOf();//'function'【4】Number types are divided into integers and floating point numbers for processing
Number.valueOf();//Number() { [native code] }typeof Number.valueOf();//'function'1. Integers directly keep up with the .valueOf() form, an error will be reported and an invalid mark is prompted, so try to add brackets
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] The valueOf() value of -0 is -0, and the value of toString() value of -0 is '0'
2. Return the original value of the floating point number
1.23.valueOf();//1.23+1.23.valueOf();//1.23-1.23.valueOf();//-1.23NaN.valueOf();//NaNInfinity.valueOf();//Infinity-Infinity.valueOf();//-Infinity
[Note] The difference between toString() is that valueOf() cannot receive conversion cardinality
【5】Object type and custom object type return to the original object
{}.valueOf();//Report an error, Unexpected token .({}).valueOf();//Object{}typeof ({}).valueOf();//'object'({a:123}).valueOf();//Object{a:123}Object.valueOf();//Object() { [native code] }typeof Object.valueOf();//'function' function Person(){ this.name = 'test';}var person1 = new Person(); person1.valueOf();//Person {name: "test"}【6】Function function type returns to the original function
function test(){ alert(1);//test}test.valueOf();/*function test(){ alert(1);//test }*/Function.valueOf();//Function() { [native code] }【7】Array type returns to the original array
[].valueOf();//[][1].valueOf();//[1][1,2,3,4].valueOf();//[1,2,3,4]Array.valueOf();//Array() { [native code] }【8】 Unlike other objects, the time Date type returns a numeric value, which is this time value
Date.now();//1465115123742(new Date()).valueOf();//1465115123742typeof (new Date()).valueOf();//'number'Date.valueOf();//Date() { [native code] }【9】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 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, while valueOf() returns the original object
2. Since undefined and null are not objects, neither of them are toString() and valueOf() methods.
3. The toString() method of numerical Number type can receive the conversion cardinality and return the numerical value in the form of a string in different digits; while the valueOf() method cannot accept the conversion cardinality
4. The toString() method of the time type returns a string representation representing time; and the valueOf() method returns a millisecond number of numerical types from now to January 1, 1970.
The above article briefly discusses the use of the String.valueOf() method is all the content I have shared with you. I hope you can give you a reference and I hope you can support Wulin.com more.