ToString () method

Source: Internet
Author: User

Previous words

This article describes the ToString () method, which returns a string that reflects the object.

"1" Undefined and null do not have the ToString () method

Undefined.tostring (); // Error null. toString (); // Error

"2" Boolean data True and False returns the corresponding ' true ' and ' false '

true. toString (); // ' true ' false. toString (); // ' false ' Boolean.tostring (); // "function Boolean () {[native code]}"

"3" String type original value returned

' 1 '. toString (); // ' 1 ' ". toString (); // "' ' ABC '. toString (); // ' abc ' String.tostring (); // "function String () {[native code]}"

The case of "4" numeric types is more complex

Number.tostring (); // "function number () {[native code]}"

1, positive floating-point number and Nan, Infinity,-infinity plus quotation marks return

1.23. toString (); // ' 1.23 ' Nan.tostring (); // ' NaN ' Infinity.tostring (); // ' Infinity '-infinity.tostring (); // '-infinity '

2, negative floating point or plus ' + ' number of the positive floating point directly follow. ToString (), ToString () is invalid and returns the original value

+1.23. toString (); // 1.23 typeof +1.23. toString (); // ' number '-1.23. toString (); // -1.23 typeof -1.23. toString (); // ' number '

3, the integer directly follow. toString () Form, will error, prompt invalid Mark

0. toString (); // uncaught syntaxerror:invalid or unexpected token

Therefore, in order to avoid the above invalid and error cases, the number when using the ToString () method, the parentheses can be resolved

(0). toString (); // ' 0 ' (-0). toString (); // ' 0 ' (+1.2). toString (); // ' 1.2 ' (-1.2). toString (); // ' -1.2 ' (NaN). toString (); // ' NaN '

Additionally, the ToString () method of the numeric type can receive an optional parameter that represents the conversion cardinality (radix), and if this parameter is not specified, the conversion rule will be based on decimal. Similarly, you can convert a number to another number (range 2-36)

var  - ; n.tostring (); // ' n.tostring '(2); // ' 10001 'n.tostring (8); // ' n.tostring '(ten); // ' n.tostring '(a); // 'n.tostring ' (+); // ' One '

"5" Object type and custom object type parentheses return [Object Object]

 {}.tostring (); //  error, unexpected token.  ({}). toString (); // [object Object]  ({a:123 }). toString (); // [object Object]  Object.ToString (); //  
function Person () {    this'test';} var New Person ();p erson1.tostring (); // "[Object Object]"

Type identification

Object.prototype.toString () is often used for type recognition and returns a string representation of the [object data type] representing the object.

Note Object.prototype.toString () recognizes standard types and built-in object types, but does not recognize custom types

Console.log (Object.prototype.toString.call ("Jerry"));//[Object String]Console.log (Object.prototype.toString.call ( A));//[Object number]Console.log (Object.prototype.toString.call (true));//[Object Boolean]Console.log (Object.prototype.toString.call (undefined));//[Object Undefined]Console.log (Object.prototype.toString.call (NULL));//[Object Null]Console.log (Object.prototype.toString.call ({name:"Jerry"}));//[Object Object]Console.log (Object.prototype.toString.call () (function () {}));//[Object Function]Console.log (Object.prototype.toString.call ([]));//[Object Array]Console.log (Object.prototype.toString.call (NewDate));//[Object Date]Console.log (Object.prototype.toString.call (/\d/));//[Object RegExp]function Person () {};console.log (Object.prototype.toString.call (Newperson));//[Object Object]
function type (obj) {returnObject.prototype.toString.call (obj). Slice (8,-1). toLowerCase ();} Console.log (Type ("Jerry"));//"string"Console.log (Type ( A));//"Number"Console.log (Type (true));//"Boolean"Console.log (type (undefined));//"undefined"Console.log (Type (NULL));//"NULL"Console.log (Type ({name:"Jerry"}));//"Object"Console.log (Type (function () {}));//"function"Console.log (Type ([]));//"Array"Console.log (Type (NewDate));//"Date"Console.log (Type (/\d/));//"RegExp"function Person () {};console.log (Type (Newperson));//"Object"

"6" Function type returns function code

function Test () {    alert (1);   Test}test.tostring (); /* "function test () {                    alert (1);//test                  }"*/function.tostring (); // "function function () {[native code]}"

"7" array type returns a comma-delimited string of strings of each value in the array

[].tostring (); // "' [1].tostring (); // ' 1 ' [1,2,3,4].tostring (); // ' 1,2,3,4 ' Array.tostring (); // "function Array () {[native code]}"

"8" Time Date type returns a string representation of the time of the current time zone

(new Date ()). ToString (); // "Sun June 10:04:53 gmt+0800 (China Standard Time)" Date.tostring (); // "function Date () {[native code]}"

"9" Regular expression regexp type returns the string representation of a regular expression literal

/ab/i.tostring (); // '/ab/i '/mom (and Dad (and baby)?)? /gi.tostring (); // ' Mom (and Dad (and baby)?)? /gi 'regexp.tostring (); // "function RegExp () {[native code]}"

"10" Error type

Error.tostring (); // "function Error () {[native code]}" Rangeerror.tostring (); // "function Rangeerror () {[native code]}" Referenceerror.tostring (); // "function Referenceerror () {[native code]}" Syntaxerror.tostring (); // "function SyntaxError () {[native code]}" Typeerror.tostring (); // "function TypeError () {[native code]}" Urierror.tostring (); // "function Urierror () {[native code]}"

ToString () method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.