Home  >  Article  >  Web Front-end  >  js data toString method_basic knowledge

js data toString method_basic knowledge

WBOY
WBOYOriginal
2016-05-16 19:16:361182browse

toString method
returns the string representation of the object.

objectname.toString([radix]) parameters
objectname
Required. To get the object represented by a string.
radix
Optional. Specifies the base when converting numeric values ​​to strings.
Description
The toString method is a member of all built-in JScript objects. Its operation depends on the type of object:

Object operation
Array Converts the elements of Array to strings. The resulting strings are comma separated and concatenated.
Boolean If the Boolean value is true, return "true". Otherwise, return "false".
Date returns the text representation of the date.
Error Returns a string containing relevant error information.
Function returns a string in the following format, where functionname is the name of the called toString method function:
function functionname( ) { [native code] }
Number returns the text representation of the number.
String returns the value of the String object.
The default returns "[object objectname]", where objectname is the name of the object type.

Example
The following example demonstrates using the toString method with a radix parameter. The return value of the function shown above is a Radix conversion table.

Copy code The code is as follows:

function CreateRadixTable (){
var s, s1, s2, s3, x;
s = "Hex Dec Binn"; // Create header.
for (x = 0; x < 16; x ) // Create numerically based on the value shown
{ switch(x) // Table size.
{// Set the space between columns. 
         case 0 :  
            s1 = "      "; 
            s2 = "    "; 
            s3 = "   "; 
            break; 
         case 1 : 
            s1 = "      "; 
            s2 = "    "; 
            s3 = "   "; 
            break; 
         case 2 : 
            s3 = "  "; 
            break; 
         case 3 :  
            s3 = "  "; 
            break; 
         case 4 :  
            s3 = " "; 
            break; 
         case 5 : 
            s3 = " "; 
            break; 
         case 6 :  
            s3 = " "; 
            break; 
         case 7 :  
            s3 = " "; 
            break; 
         case 8 : 
            s3 = "" ; 
            break; 
         case 9 : 
            s3 = ""; 
            break; 
         default:  
            s1 = "     "; 
            s2 = ""; 
            s3 = "    "; 
      }                                     // 转换为十六进制、十进制、二进制。 
      s  = " "   x.toString(16)   s1   x.toString(10) 
      s  =  s2   s3   x.toString(2)  "n"; 

   } 
   return(s);                               // 返回整个 radix 表。 
}
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