溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

toString()方法

發(fā)布時間:2020-07-19 16:43:18 來源:網(wǎng)絡(luò) 閱讀:423 作者:jjjyyy66 欄目:網(wǎng)絡(luò)安全

【1】undefined和null沒有toString()方法

undefined.toString();//錯誤null.toString();//錯誤

 

【2】布爾型數(shù)據(jù)true和false返回對應(yīng)的'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】數(shù)值類型的情況較復(fù)雜

Number.toString();//"function Number() { [native code] }"

  1、正浮點數(shù)及NaN、Infinity、-Infinity加引號返回

1.23.toString();//'1.23'NaN.toString();//'NaN'Infinity.toString();//'Infinity'-Infinity.toString();//'-Infinity'

  2、負浮點數(shù)或加'+'號的正浮點數(shù)直接跟上.toString(),toString()無效并返回原數(shù)值

+1.23.toString();//1.23typeof +1.23.toString();//'number'-1.23.toString();//-1.23typeof -1.23.toString();//'number'

  3、整數(shù)直接跟上.toString()形式,會報錯,提示無效標記,因為整數(shù)后的點會被識別為小數(shù)點

0.toString();//Uncaught SyntaxError: Invalid or unexpected token

  因此,為了避免以上無效及報錯的情況,數(shù)字在使用toString()方法時,加括號可解決

(0).toString();//'0'(-0).toString();//'0'(+1.2).toString();//'1.2'(-1.2).toString();//'-1.2'(NaN).toString();//'NaN'

  此外,數(shù)字類型的toString()方法可以接收表示轉(zhuǎn)換基數(shù)(radix)的可選參數(shù),如果不指定此參數(shù),轉(zhuǎn)換規(guī)則將是基于十進制。同樣,也可以將數(shù)字轉(zhuǎn)換為其他進制數(shù)(范圍在2-36)

toString()方法

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'

toString()方法

 

【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 數(shù)據(jù)類型]字符串表示

  [注意]Object.prototype.toString()可以識別標準類型及內(nèi)置對象類型,但不能識別自定義類型

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]

toString()方法

toString()方法

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"

toString()方法

 

【6】函數(shù)Function類型返回函數(shù)代碼

  當(dāng)我們對一個自定義函數(shù)調(diào)用toString()方法時,可以得到該函數(shù)的源代碼;如果對內(nèi)置函數(shù)使用toString()方法時,會得到一個'[native code]'字符串。因此,可以使用toString()方法來區(qū)分自定義函數(shù)和內(nèi)置函數(shù)

toString()方法

function test(){
    alert(1);//test}
test.toString();/*"function test(){
                    alert(1);//test
                  }"*/Function.toString();//"function Function() { [native code] }"

toString()方法

 

【7】數(shù)組Array類型返回由數(shù)組中每個值的字符串形式拼接而成的一個以逗號分隔的字符串

[].toString();//''[1].toString();//'1'[1,2,3,4].toString();//'1,2,3,4'Array.toString();//"function Array() { [native code] }"

 

【8】時間Date類型返回表示當(dāng)前時區(qū)的時間的字符串表示

(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類型

toString()方法

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] }"

toString()方法

 


向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI