文章出處

前面的話

  本文將介紹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"

其他識別

  除了類型識別之外,還可以進行其他識別,如識別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] }"

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜

    大師兄 發表在 痞客邦 留言(0) 人氣()