js資料toString 方法

來源:互聯網
上載者:User

toString 方法
返回對象的字串表示。

objectname.toString([radix])參數
objectname
必選項。要得到字串表示的對象。
radix
可選項。指定將數字值轉換為字串時的進位。
說明
toString 方法是所有內建的 JScript 對象的成員。它的操作依賴於對象的類型:

對象 操作
Array 將 Array 的元素轉換為字串。結果字串由逗號分隔,且串連起來。
Boolean 如果 Boolean 值是 true,則返回 “true”。否則,返回 “false”。
Date 返回日期的文字標記法。
Error 返回一個包含相關錯誤資訊的字串。
Function 返回如下格式的字串,其中 functionname 是被調用 toString 方法函數的名稱:
function functionname( ) { [native code] }
Number 返回數位文字表示。
String 返回 String 對象的值。
預設 返回 “[object objectname]”,其中 objectname 是物件類型的名稱。

樣本
下面的例子示範了使用帶有 radix 參數的 toString 方法。上面所示函數的傳回值是一個 Radix 轉換表。 複製代碼 代碼如下:function CreateRadixTable (){
var s, s1, s2, s3, x; // 聲明變數。
s = "Hex Dec Bin \n"; // 建立表頭。
for (x = 0; x < 16; x++) // 根據所示值的
{ // 數字建立
switch(x) // 表尺寸。
{ // 設定欄目間空間。
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 表。
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.