Condividi tramite


Metodo toString

Aggiornamento: novembre 2007

Restituisce la rappresentazione in forma di stringa di un oggetto.

function toString( [radix : Number] ) : String

Argomenti

  • radix
    Facoltativo. Consente di specificare una radice per la conversione di valori numerici in stringhe. Questo valore è utilizzato solo per i numeri.

Note

Il metodo toString è un membro di tutti gli oggetti incorporati JScript. Il suo funzionamento dipende dal tipo di oggetto.

Oggetto

Comportamento

Array

Gli elementi di un oggetto Array vengono convertiti in stringhe. Le stringhe risultati sono concatenate, separate da virgole.

Boolean

Se il valore booleano è true, viene restituito "true". In caso contrario viene restituito "false".

Date

Viene restituita la rappresentazione della data in forma di testo.

Error

Viene restituita una stringa contenente il messaggio di errore associato.

Funzione

Viene restituita una stringa nel formato indicato di seguito, dove functionname indica il nome della funzione di cui viene richiamato il metodo toString:

"function functionname() { [native code] }"

Number

Viene restituita la rappresentazione del numero in forma di testo.

String

Viene restituito il valore dell'oggetto String.

Default

Viene restituito "[object objectname]", dove objectname è il nome del tipo di oggetto.

Esempio

Nel codice seguente viene illustrato l'utilizzo del metodo toString con un argomento radix. Il valore restituito dalla funzione illustrata di seguito è una tabella di conversione delle radici.

function CreateRadixTable (){
   var s, s1, s2, s3, x;                    //Declare variables.
   s = "Hex    Dec   Bin \n";               //Create table heading.
   for (x = 0; x < 16; x++)                 //Establish size of table
   {                                        // in terms of number of
      switch(x)                             // values shown.
      {                                     //Set intercolumn spacing.
         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 = "    ";
      }                                     //Convert to hex, decimal & binary.
      s += " " + x.toString(16) + s1 + x.toString(10)
      s +=  s2 + s3 + x.toString(2)+ "\n";
      
   }
   return(s);                               //Return entire radix table.
}

Requisiti

Versione 2

Si applica a:

Oggetto Array| Oggetto Boolean| Oggetto Date| Oggetto Error| Oggetto Function| Oggetto Number| Oggetto Object| Oggetto String

Vedere anche

Riferimenti

Istruzione function