- toString()方法可以根據所傳遞的參數把數值轉換為對應進制的數字字符串。參數范圍為 2~36 之間的任意整數。
var a = 32; console.log(a.toString(2)); //返回字符串100000 console.log(a.toString(4)); //返回字符串200 console.log(a.toString(16)); //返回字符串20 console.log(a.toString(30)); //返回字符串12 console.log(a.toString(32)); //返回字符串10
- 數值直接量不能直接調用 toString() 方法,必須先使用小括號或其他方法轉化數字。
console.log(123.toString()); //拋出語法錯誤 相當於打印 123.0toString() console.log(1.23.toString()); //返回123 console.log((123).toString()); //返回123 console.log(123 .toString()); //返回123 var a = 123; console.log(a.toString()); //返回123
注意
1.undefined和null沒有toString()方法
2.布爾型數據true和false返回對應的’true’和’false’
3.字符串類型原值返回
4.數值類型的情況較復雜
4.1、正浮點數及NaN、Infinity加引號返回
1.23.toString();//'1.23' NaN.toString();//'NaN' Infinity.toString();//'Infinity'
4.2、負浮點數或加’+'號的正浮點數直接跟上.toString(),相當於先運行toString()方法,再添加正負號,轉換為數字
+1.23.toString();//1.23 typeof +1.23.toString();//'number' -1.23.toString();//-1.23 typeof -1.23.toString();//'number'
4.3、整數直接跟上.toString()形式,會報錯,提示無效標記,因為整數后的點會被識別為小數點
0.toString();//Uncaught SyntaxError: Invalid or unexpected token
因此,為了避免以上無效及報錯的情況,數字在使用toString()方法時,加括號可解決
(0).toString();//'0' (-0).toString();//'0' (+1.2).toString();//'1.2' (-1.2).toString();//'-1.2' (NaN).toString();//'NaN'
此外,數字類型的toString()方法可以接收表示轉換基數(radix)的可選參數,如果不指定此參數,轉換規則將是基於十進制。同樣,也可以將數字轉換為其他進制數(范圍在2-36)
var n = 17; n.toString();//'17' n.toString(2);//'10001' n.toString(8);//'21' n.toString(10);//'17' n.toString(12);//'15' n.toString(16);//'11'
(5)對象Object類型及自定義對象類型加括號返回[object Object]
{}.toString();//報錯,Unexpected token . ({}).toString();//[object Object] ({a:123}).toString();//[object Object] Object.toString();//"function Object() { [native code] }"
function Person(){ this.name = 'test'; } var person1 = new Person(); person1.toString();//"[object Object]"
類型識別
常常使用Object.prototype.toString()來進行類型識別,返回代表該對象的[object 數據類型]字符串表示
[注意]Object.prototype.toString()可以識別標准類型及內置對象類型,但不能識別自定義類型
console.log(Object.prototype.toString.call("jerry"));//[object String] console.log(Object.prototype.toString.call(12));//[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(new Date));//[object Date] console.log(Object.prototype.toString.call(/\d/));//[object RegExp] function Person(){}; console.log(Object.prototype.toString.call(new Person));//[object Object]
function type(obj){ return Object.prototype.toString.call(obj).slice(8,-1).toLowerCase(); } console.log(type("jerry"));//"string" console.log(type(12));//"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(new Date));//"date" console.log(type(/\d/));//"regexp" function Person(){}; console.log(type(new Person));//"object"
其他識別
除了類型識別之外,還可以進行其他識別,如識別arguments或DOM元素
(function(){ console.log(Object.prototype.toString.call(arguments));//[object Arguments] })() console.log(Object.prototype.toString.call(document));//[object HTMLDocument]
(6)函數Function類型返回函數代碼
當我們對一個自定義函數調用toString()方法時,可以得到該函數的源代碼;如果對內置函數使用toString()方法時,會得到一個’[native code]'字符串。因此,可以使用toString()方法來區分自定義函數和內置函數
function test(){ alert(1);//test } test.toString();/*"function test(){ alert(1);//test }"*/ Function.toString();//"function Function() { [native code] }"
(7)數組Array類型返回由數組中每個值的字符串形式拼接而成的一個以逗號分隔的字符串
[].toString();//'' [1].toString();//'1' [1,2,3,4].toString();//'1,2,3,4' Array.toString();//"function Array() { [native code] }"
(8)時間Date類型返回表示當前時區的時間的字符串表示
(new Date()).toString();//"Sun Jun 05 2016 10:04:53 GMT+0800 (中國標准時間)" Date.toString();//"function Date() { [native code] }"
(9)正則表達式RegExp類型返回正則表達式字面量的字符串表示
/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類型
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] }"