[1]Undefined、Null [2]Boolean [3]String [4]Number [5]Object [6]Function [7]Array [8]Date [9]RegExp [10]Error ...
前面的話
本文將介紹toString()方法,toString()方法返回反映這個對象的字元串
【1】undefined和null沒有toString()方法
undefined.toString();//錯誤 null.toString();//錯誤
【2】布爾型數據true和false返回對應的'true'和'false'
true.toString();//'true' false.toString();//'false' Boolean.toString();//"function Boolean() { [native code] }"
【3】字元串類型原值返回
'1'.toString();//'1' ''.toString();//'' 'abc'.toString();//'abc' String.toString();//"function String() { [native code] }"
【4】數值類型的情況較複雜
Number.toString();//"function Number() { [native code] }"
1、正浮點數及NaN、Infinity、-Infinity加引號返回
1.23.toString();//'1.23' NaN.toString();//'NaN' Infinity.toString();//'Infinity' -Infinity.toString();//'-Infinity'
2、負浮點數或加'+'號的正浮點數直接跟上.toString(),toString()無效並返回原數值
+1.23.toString();//1.23 typeof +1.23.toString();//'number' -1.23.toString();//-1.23 typeof -1.23.toString();//'number'
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"
【6】函數Function類型返回函數代碼
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] }"