老生常談javascript的類型轉(zhuǎn)換
目錄:
1 : 偽對(duì)象
2 : 轉(zhuǎn)換為字符串
3 : 數(shù)字轉(zhuǎn)字符串
4 : 轉(zhuǎn)換為數(shù)字
5 : 轉(zhuǎn)換為Boolean
6 : Number()和parseInt()的區(qū)別
7 : String()和toString()的區(qū)別
1 : 偽對(duì)象
偽對(duì)象:javascript是一門很有意思的語(yǔ)言,即便是基本類型,也是偽對(duì)象,所以他們都有屬性和方法。
變量a的類型是字符串,通過調(diào)用其為偽對(duì)象的屬性length獲取其長(zhǎng)度 。
<script>
var a="hello javascript";
document.write("變量a的類型是:"+(typeof a));
document.write("<br>");
document.write("變量a的長(zhǎng)度是:"+a.length);
</script>
運(yùn)行效果:
變量a的類型是:string
變量a的長(zhǎng)度是:16
2 : 轉(zhuǎn)換為字符串
無論是Number,Boolean還是String都有一個(gè)toString方法,用于轉(zhuǎn)換為字符串
<script>
var a=10;
document.write("數(shù)字 "+a+" 轉(zhuǎn)換為字符串"+a.toString());
document.write("<br>");
var b=true;
document.write("布爾 "+b+" 轉(zhuǎn)換為字符串"+b.toString());
document.write("<br>");
var c="hello javascript";
document.write("字符串 "+c+" 轉(zhuǎn)換為字符串 "+c.toString());
document.write("<br>");
</script>
運(yùn)行效果:
數(shù)字 10 轉(zhuǎn)換為字符串10
布爾 true 轉(zhuǎn)換為字符串true
字符串 hello javascript 轉(zhuǎn)換為字符串 hello javascript
3 : 數(shù)字轉(zhuǎn)字符串
Number轉(zhuǎn)換為字符串的時(shí)候有默認(rèn)模式和基模式兩種
<script>
var a=10;
document.write('默認(rèn)模式下,數(shù)字10轉(zhuǎn)換為十進(jìn)制的'+a.toString()); //默認(rèn)模式,即十進(jìn)制
document.write("<br>");
document.write('基模式下,數(shù)字10轉(zhuǎn)換為二進(jìn)制的'+a.toString(2)); //基模式,二進(jìn)制
document.write("<br>");
document.write('基模式下,數(shù)字10轉(zhuǎn)換為八進(jìn)制的'+a.toString(8)); //基模式,八進(jìn)制
document.write("<br>");
document.write('基模式下,數(shù)字10轉(zhuǎn)換為十六進(jìn)制的'+a.toString(16)); //基模式,十六進(jìn)制
document.write("<br>");
</script>
運(yùn)行效果:
默認(rèn)模式下,數(shù)字10轉(zhuǎn)換為十進(jìn)制的10
基模式下,數(shù)字10轉(zhuǎn)換為二進(jìn)制的1010
基模式下,數(shù)字10轉(zhuǎn)換為八進(jìn)制的12
基模式下,數(shù)字10轉(zhuǎn)換為十六進(jìn)制的a
4 : 轉(zhuǎn)換為數(shù)字
javascript分別提供內(nèi)置函數(shù) parseInt()和parseFloat(),轉(zhuǎn)換為數(shù)字
注:如果被轉(zhuǎn)換的字符串,同時(shí)又?jǐn)?shù)字和字符構(gòu)成,那么parseInt會(huì)一直定位數(shù)字,直到出現(xiàn)非字符。 所以"10abc" 會(huì)被轉(zhuǎn)換為 10
思考題: 字符串"10abc8" 又會(huì)被轉(zhuǎn)換為多少呢?
<script>
document.write("字符串的\"10\"轉(zhuǎn)換為數(shù)字的:"+parseInt("10")); //轉(zhuǎn)換整數(shù)
document.write("<br>");
document.write("字符串的\"3.14\"轉(zhuǎn)換為數(shù)字的:"+parseFloat("444 3.14"));//轉(zhuǎn)換浮點(diǎn)數(shù)
document.write("<br>");
document.write("字符串的\"10abc\"轉(zhuǎn)換為數(shù)字的:"+parseInt("10abc")); //判斷每一位,直到發(fā)現(xiàn)不是數(shù)字的那一位
document.write("<br>");
document.write("字符串的\"hello javascript\"轉(zhuǎn)換為數(shù)字的:"+parseInt("h5555ello javascript")); //如果完全不包含數(shù)字,則返
回NaN - Not a Number
document.write("<br>");
</script>
運(yùn)行效果:
字符串的"10"轉(zhuǎn)換為數(shù)字的:10
字符串的"3.14"轉(zhuǎn)換為數(shù)字的:444
字符串的"10abc"轉(zhuǎn)換為數(shù)字的:10
字符串的"hello javascript"轉(zhuǎn)換為數(shù)字的:NaN
5 : 轉(zhuǎn)換為Boolean
使用內(nèi)置函數(shù)Boolean() 轉(zhuǎn)換為Boolean值
當(dāng)轉(zhuǎn)換字符串時(shí):
非空即為true
當(dāng)轉(zhuǎn)換數(shù)字時(shí):
非0即為true
當(dāng)轉(zhuǎn)換對(duì)象時(shí):
非null即為true
<script>
document.write("空字符串''轉(zhuǎn)換為布爾后的值:"+Boolean("")); //空字符串
document.write("<br>");
document.write("非空字符'hello javascript '串轉(zhuǎn)換為布爾后的值:"+Boolean("hello javascript")); //非空字符串
document.write("<br>");
document.write("數(shù)字 0 轉(zhuǎn)換為布爾后的值:"+Boolean(0)); //0
document.write("<br>");
document.write("數(shù)字 3.14 轉(zhuǎn)換為布爾后的值:"+Boolean(3.14)); //非0
document.write("<br>");
document.write("空對(duì)象 null 轉(zhuǎn)換為布爾后的值:"+Boolean(null)); //null
document.write("<br>");
document.write("非對(duì)象 new Object() 轉(zhuǎn)換為布爾后的值:"+Boolean(new Object())); //對(duì)象存在
document.write("<br>");
</script>
運(yùn)行效果:
空字符串''轉(zhuǎn)換為布爾后的值:false
非空字符'hello javascript '串轉(zhuǎn)換為布爾后的值:true
數(shù)字 0 轉(zhuǎn)換為布爾后的值:false
數(shù)字 3.14 轉(zhuǎn)換為布爾后的值:true
空對(duì)象 null 轉(zhuǎn)換為布爾后的值:false
非對(duì)象 new Object() 轉(zhuǎn)換為布爾后的值:true
6 : Number()和parseInt()的區(qū)別
Number()和parseInt()一樣,都可以用來進(jìn)行數(shù)字的轉(zhuǎn)換
區(qū)別在于,當(dāng)轉(zhuǎn)換的內(nèi)容包含非數(shù)字的時(shí)候,Number() 會(huì)返回NaN(Not a Number)
parseInt() 要看情況,如果以數(shù)字開頭,就會(huì)返回開頭的合法數(shù)字部分,如果以非數(shù)字開頭,則返回NaN
<script>
document.write("通過Number() 函數(shù)轉(zhuǎn)換字符串'123' 后得到的數(shù)字:"+Number("123")); //正常的
document.write("<br>");
document.write("通過Number() 函數(shù)轉(zhuǎn)換字符串'123abc' 后得到的數(shù)字:"+Number("123abc")); //包含非數(shù)字
document.write("<br>");
document.write("通過Number() 函數(shù)轉(zhuǎn)換字符串'abc123' 后得到的數(shù)字:"+Number("abc123")); //包含非數(shù)字
document.write("<br>");
document.write("通過parseInt() 函數(shù)轉(zhuǎn)換字符串'123' 后得到的數(shù)字:"+parseInt("123")); //正常的
document.write("<br>");
document.write("通過parseInt() 函數(shù)轉(zhuǎn)換字符串'123abc' 后得到的數(shù)字:"+parseInt("123abc")); //包含非數(shù)字,返回開頭的合法
數(shù)字部分
document.write("<br>");
document.write("通過parseInt() 函數(shù)轉(zhuǎn)換字符串'abc123' 后得到的數(shù)字:"+parseInt("abc123")); //包含非數(shù)字,以非數(shù)字開頭,
返回NaN
document.write("<br>");
</script>
運(yùn)行效果:
通過Number() 函數(shù)轉(zhuǎn)換字符串'123' 后得到的數(shù)字:123
通過Number() 函數(shù)轉(zhuǎn)換字符串'123abc' 后得到的數(shù)字:NaN
通過Number() 函數(shù)轉(zhuǎn)換字符串'abc123' 后得到的數(shù)字:NaN
通過parseInt() 函數(shù)轉(zhuǎn)換字符串'123' 后得到的數(shù)字:123
通過parseInt() 函數(shù)轉(zhuǎn)換字符串'123abc' 后得到的數(shù)字:123
通過parseInt() 函數(shù)轉(zhuǎn)換字符串'abc123' 后得到的數(shù)字:NaN
7 : String()和toString()的區(qū)別
String()和toString()一樣都會(huì)返回字符串,區(qū)別在于對(duì)null的處理
String()會(huì)返回字符串"null"
toString() 就會(huì)報(bào)錯(cuò),無法執(zhí)行
<script>
var a = null;
document.write('String(null) 把空對(duì)象轉(zhuǎn)換為字符串:'+String(a));
document.write("<br>");
document.write('null.toString() 就會(huì)報(bào)錯(cuò),所以后面的代碼不能執(zhí)行');
document.write(a.toString());
document.write("因?yàn)榈?行報(bào)錯(cuò),所以這一段文字不會(huì)顯示");
</script>
運(yùn)行效果:
String(null) 把空對(duì)象轉(zhuǎn)換為字符串:null
null.toString() 就會(huì)報(bào)錯(cuò),所以后面的代碼不能執(zhí)行
以上就是小編為大家?guī)淼睦仙U刯avascript的類型轉(zhuǎn)換全部?jī)?nèi)容了,希望大家多多支持腳本之家~
相關(guān)文章
JavaScript模擬實(shí)現(xiàn)自由落體效果
這篇文章主要為大家詳細(xì)介紹了JavaScript模擬實(shí)現(xiàn)自由落體效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
JS實(shí)現(xiàn)獲取鍵盤按下的按鍵并顯示在頁(yè)面上的方法
這篇文章主要介紹了JS實(shí)現(xiàn)獲取鍵盤按下的按鍵并顯示在頁(yè)面上的方法,涉及JavaScript針對(duì)鍵盤事件及頁(yè)面元素的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
JS無法捕獲滾動(dòng)條上的mouse up事件的原因猜想
當(dāng)用戶鼠標(biāo)在滾動(dòng)條上按下的時(shí)候,我們可以假設(shè)他(她)正在瀏覽聊天內(nèi)容,那么這個(gè)時(shí)候好的用戶體驗(yàn)就不能讓滾動(dòng)條再自動(dòng)滾動(dòng)了2012-03-03
js使用for循環(huán)與innerHTML獲取選中tr下td值
這篇文章主要與大家分享了js使用for循環(huán)與innerHTML獲取選中tr下td值的方法,很簡(jiǎn)單,但很實(shí)用,有需要的朋友可以參考下2014-09-09
uniapp項(xiàng)目實(shí)踐自定義加載組件示例詳解
這篇文章主要為大家介紹了uniapp項(xiàng)目實(shí)踐自定義加載組件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
解決layer彈出層的內(nèi)容頁(yè)點(diǎn)擊按鈕跳轉(zhuǎn)到新的頁(yè)面問題
今天小編就為大家分享一篇解決layer彈出層的內(nèi)容頁(yè)點(diǎn)擊按鈕跳轉(zhuǎn)到新的頁(yè)面問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09
javascript結(jié)合html5 canvas實(shí)現(xiàn)(可調(diào)畫筆顏色/粗細(xì)/橡皮)的涂鴉板
js+html5 canvas實(shí)現(xiàn)的涂鴉畫板特效,可調(diào)畫筆顏色|粗細(xì)|橡皮,可以保存涂鴉效果為圖片編碼,測(cè)試了下還不錯(cuò),感興趣的朋友可以參考下2013-04-04

