Home >Web Front-end >JS Tutorial >Detailed explanation of the use of String.valueOf() method

Detailed explanation of the use of String.valueOf() method

亚连
亚连Original
2018-05-21 11:02:363827browse

Now I will bring you a brief discussion on the use of String.valueOf() method. Let me share it with you now and give it as a reference for everyone.

The previous words

Regarding type conversion, the two common methods of objects are toString() and valueOf(). In fact, these two methods can also be applied to packaging types. The toString() method has been introduced before. This article will introduce the valueOf() method, which returns the original value

[1] There is no valueOf() method for undefined and null

undefined.valueOf();//错误
null.valueOf();//错误

【2】Boolean data true and false return 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 return

'1'.valueOf();//'1'
''.valueOf();//''
'abc'.valueOf();//'abc'
String.valueOf();//String() { [native code] }
typeof String.valueOf();//'function'

[4] Numerical types are divided into integers and floating point numbers for processing

Number.valueOf();//Number() { [native code] }
typeof Number.valueOf();//'function'

1. The integer directly follows the .valueOf() form, an error will be reported, and an invalid mark will be 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 toString() value of -0 is '0'

2. Floating point numbers The original value is returned

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() cannot receive the conversion base

[5] Object Object type and customization Object type returns the original object

{}.valueOf();//报错,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 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] Unlike other objects, the time Date type returns a numeric value, which is this time value

Date.now();//1465115123742
(new Date()).valueOf();//1465115123742
typeof (new Date()).valueOf();//'number'
Date.valueOf();//Date() { [native code] }

[9] Regular The 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)?)?/gi
RegExp.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] }

Summary

1. The main difference between toString() and valueOf() is that toString() returns a string, while valueOf() returns the original object

2. Due to undefined and null is not an object, so they have neither toString() nor valueOf() methods

3. The toString() method of the Numeric Number type can receive the conversion base and return values ​​in the form of strings in different bases; The valueOf() method cannot accept the conversion base

4. The toString() method of the time Date type returns a string representation of the time; and the valueOf() method returns the time from now to January 1, 1970. The number of milliseconds in the numerical type of 00:00:00

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Detailed explanation of the use of AJAX and JavaScript

How to use ajax to operate forms with JavaScript

What is the difference between .min.js and .js files in javascript?

The above is the detailed content of Detailed explanation of the use of String.valueOf() method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn