toString()和valueOf()都是對(duì)象的方法。toString()方法返回反映這個(gè)對(duì)象的字符串。valueOf()方法如果存在任意原始值,它就默認(rèn)將對(duì)象轉(zhuǎn)換為表示它的原始值;對(duì)象是復(fù)合值,而大多數(shù)對(duì)象無(wú)法真正表示為一個(gè)原始值,因此默認(rèn)的valueOf()方法簡(jiǎn)單地返回對(duì)象本身,而不是返回一個(gè)原始值。
【1】undefined和null沒(méi)有toString()和valueOf()方法
undefined.toString();//錯(cuò)誤
null.toString();//錯(cuò)誤
undefined.valueOf();//錯(cuò)誤
null.valueOf();//錯(cuò)誤
【2】布爾型類型
true.toString();//'true'
false.toString();//'false'
Boolean.toString();//"function Boolean() { [native code] }"
typeof Boolean.toString();//string
true.valueOf();//true
false.valueOf();//false
Boolean.valueOf();//Boolean() { [native code] }
typeof Boolean.valueOf();//'function'
【3】字符串類型
'1'.toString();//'1'
''.toString();//''
'abc'.toString();//'abc'
String.toString();//"function String() { [native code] }"
'1'.valueOf();//'1'
''.valueOf();//''
'abc'.valueOf();//'abc'
String.valueOf();//String() { [native code] }
typeof String.valueOf();//'function'
【4】數(shù)值類型
Number.toString();//"function Number() { [native code] }"
toString()對(duì)于數(shù)值的轉(zhuǎn)換,會(huì)直接轉(zhuǎn)換成字符串,如果數(shù)值前面有(+)則會(huì)省略。
整數(shù)直接跟上.toString()形式,會(huì)報(bào)錯(cuò),提示無(wú)效標(biāo)記,因?yàn)檎麛?shù)后的點(diǎn)會(huì)被識(shí)別為小數(shù)點(diǎn)
0.toString();//Uncaught SyntaxError: Invalid or unexpected token
可以這樣寫:
(0).toString();//'0'
Number.valueOf();//Number() { [native code] }
typeof Number.valueOf();//'function'
[注意]-0的valueOf()值是-0,而-0的toString()值是'0'
對(duì)于數(shù)值的注意寫法同上
【5】對(duì)象Object類型及自定義對(duì)象類型
{}.toString();//報(bào)錯(cuò),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()來(lái)進(jìn)行類型識(shí)別,返回代表該對(duì)象的[object 數(shù)據(jù)類型]字符串表示
[注意]Object.prototype.toString()可以識(shí)別標(biāo)準(zhǔn)類型及內(nèi)置對(duì)象類型,但不能識(shí)別自定義類型
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]
除了類型識(shí)別之外,還可以進(jìn)行其他識(shí)別,如識(shí)別arguments或DOM元素
(function(){
console.log(Object.prototype.toString.call(arguments));//[object Arguments]
})()
console.log(Object.prototype.toString.call(document));//[object HTMLDocument]
valueOf()
{}.valueOf();//報(bào)錯(cuò),Unexpected token .
({}).valueOf();//Object{}
typeof ({}).valueOf();//'object'
({a:123}).valueOf();//Object{a:123}
Object.valueOf();//Object() { [native code] }
typeof Object.valueOf();//'function'
function Person(){
this.name = 'test';
}
var person1 = new Person();
person1.valueOf();//Person {name: "test"}
【6】函數(shù)Function類型
toString()
函數(shù)Function類型返回函數(shù)代碼,當(dāng)我們對(duì)一個(gè)自定義函數(shù)調(diào)用toString()方法時(shí),可以得到該函數(shù)的源代碼;如果對(duì)內(nèi)置函數(shù)使用toString()方法時(shí),會(huì)得到一個(gè)'[native code]'字符串。因此,可以使用toString()方法來(lái)區(qū)分自定義函數(shù)和內(nèi)置函數(shù)
function test(){
alert(1);//test
}
test.toString();/*"function test(){
alert(1);//test
}"*/
Function.toString();//"function Function() { [native code] }"
valueOf()
函數(shù)Function類型返回原函數(shù)
function test(){
alert(1);//1
}
test.valueOf();/*function test(){
alert(1);//1
}*/
Function.valueOf();//Function() { [native code] }
【7】數(shù)組Array類型
toString()
數(shù)組Array類型返回由數(shù)組中每個(gè)值的字符串形式拼接而成的一個(gè)以逗號(hào)分隔的字符串
[].toString();//''
[1].toString();//'1'
[1,2,3,4].toString();//'1,2,3,4'
Array.toString();//"function Array() { [native code] }"
valueOf()
數(shù)組Array類型返回原數(shù)組
[].valueOf();//[]
[1].valueOf();//[1]
[1,2,3,4].valueOf();//[1,2,3,4]
Array.valueOf();//Array() { [native code] }
【8】時(shí)間Date類型
toString()
時(shí)間Date類型返回表示當(dāng)前時(shí)區(qū)的時(shí)間的字符串表示
(new Date()).toString();//"Sun Jun 10 2017 10:04:53 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)"
Date.toString();//"function Date() { [native code] }"
valueOf()
和其他對(duì)象不同,時(shí)間Date類型返回一個(gè)數(shù)字值,它是當(dāng)前時(shí)間值
Date.now();//1465115123742
(new Date()).valueOf();//1465115123742
typeof (new Date()).valueOf();//'number'
Date.valueOf();//Date() { [native code] }
【9】正則表達(dá)式RegExp類型
toString()
正則表達(dá)式RegExp類型返回正則表達(dá)式字面量的字符串表示
/ab/i.toString();//'/ab/i'
/mom( and dad( and baby)?)?/gi.toString();//'mom( and dad( and baby)?)?/gi'
RegExp.toString();//"function RegExp() { [native code] }"
valueOf()
正則表達(dá)式RegExp類型返回原正則對(duì)象
/ab/i.valueOf();///ab/i
/mom( and dad( and baby)?)?/gi.valueOf();//mom( and dad( and baby)?)?/gi
RegExp.valueOf();//RegExp() { [native code] }
錯(cuò)誤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] }"
Error.valueOf();//Error() { [native code] }
RangeError.valueOf();//RangeError() { [native code] }
ReferenceError.valueOf();//ReferenceError() { [native code] }
SyntaxError.valueOf();//SyntaxError() { [native code] }
TypeError.valueOf();//TypeError() { [native code] }
URIError.valueOf();//URIError() { [native code] }
總結(jié)
1、toString()和valueOf()的主要不同點(diǎn)在于,toString()返回的是字符串,而valueOf()返回的是原對(duì)象
2、由于undefined和null不是對(duì)象,所以它們toString()和valueOf()兩個(gè)方法都沒(méi)有
3、數(shù)值Number類型的toString()方法可以接收轉(zhuǎn)換基數(shù),返回不同進(jìn)制的字符串形式的數(shù)值;而valueOf()方法無(wú)法接受轉(zhuǎn)換基數(shù)
4、時(shí)間Date類型的toString()方法返回的表示時(shí)間的字符串表示;而valueOf()方法返回的是現(xiàn)在到1970年1月1日00:00:00的數(shù)值類型的毫秒數(shù)
5、包裝對(duì)象的valueOf()方法返回該包裝對(duì)象對(duì)應(yīng)的原始值
6、使用toString()方法可以區(qū)分內(nèi)置函數(shù)和自定義函數(shù)