老生常談javascript的類型轉(zhuǎn)換
目錄:
1 : 偽對象
2 : 轉(zhuǎn)換為字符串
3 : 數(shù)字轉(zhuǎn)字符串
4 : 轉(zhuǎn)換為數(shù)字
5 : 轉(zhuǎn)換為Boolean
6 : Number()和parseInt()的區(qū)別
7 : String()和toString()的區(qū)別
1 : 偽對象
偽對象:javascript是一門很有意思的語言,即便是基本類型,也是偽對象,所以他們都有屬性和方法。
變量a的類型是字符串,通過調(diào)用其為偽對象的屬性length獲取其長度 。
<script> var a="hello javascript"; document.write("變量a的類型是:"+(typeof a)); document.write("<br>"); document.write("變量a的長度是:"+a.length); </script>
運(yùn)行效果:
變量a的類型是:string
變量a的長度是:16
2 : 轉(zhuǎn)換為字符串
無論是Number,Boolean還是String都有一個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)換為字符串的時候有默認(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)換的字符串,同時又?jǐn)?shù)字和字符構(gòu)成,那么parseInt會一直定位數(shù)字,直到出現(xiàn)非字符。 所以"10abc" 會被轉(zhuǎn)換為 10
思考題: 字符串"10abc8" 又會被轉(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)換浮點數(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)換字符串時:
非空即為true
當(dāng)轉(zhuǎn)換數(shù)字時:
非0即為true
當(dāng)轉(zhuǎn)換對象時:
非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("空對象 null 轉(zhuǎn)換為布爾后的值:"+Boolean(null)); //null document.write("<br>"); document.write("非對象 new Object() 轉(zhuǎn)換為布爾后的值:"+Boolean(new Object())); //對象存在 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
空對象 null 轉(zhuǎn)換為布爾后的值:false
非對象 new Object() 轉(zhuǎn)換為布爾后的值:true
6 : Number()和parseInt()的區(qū)別
Number()和parseInt()一樣,都可以用來進(jìn)行數(shù)字的轉(zhuǎn)換
區(qū)別在于,當(dāng)轉(zhuǎn)換的內(nèi)容包含非數(shù)字的時候,Number() 會返回NaN(Not a Number)
parseInt() 要看情況,如果以數(shù)字開頭,就會返回開頭的合法數(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()一樣都會返回字符串,區(qū)別在于對null的處理
String()會返回字符串"null"
toString() 就會報錯,無法執(zhí)行
<script> var a = null; document.write('String(null) 把空對象轉(zhuǎn)換為字符串:'+String(a)); document.write("<br>"); document.write('null.toString() 就會報錯,所以后面的代碼不能執(zhí)行'); document.write(a.toString()); document.write("因為第5行報錯,所以這一段文字不會顯示"); </script>
運(yùn)行效果:
String(null) 把空對象轉(zhuǎn)換為字符串:null
null.toString() 就會報錯,所以后面的代碼不能執(zhí)行
以上就是小編為大家?guī)淼睦仙U刯avascript的類型轉(zhuǎn)換全部內(nèi)容了,希望大家多多支持腳本之家~
相關(guān)文章
uniapp使用scroll-view實現(xiàn)左右上下滑動功能
最近在用uni-app開發(fā)小程序時,需要用scroll-view做出左右上下滑動效果,所以下面這篇文章主要給大家介紹了關(guān)于uniapp使用scroll-view實現(xiàn)左右上下滑動功能的相關(guān)資料,需要的朋友可以參考下2022-11-11利用原生JavaScript實現(xiàn)造日歷輪子實例代碼
這篇文章主要給大家介紹了關(guān)于如何利用原生JavaScript實現(xiàn)造日歷輪子的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用JavaScript具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05整理Javascript數(shù)組學(xué)習(xí)筆記
整理Javascript數(shù)組學(xué)習(xí)筆記,之前一系列的文章是跟我學(xué)習(xí)Javascript,本文就是進(jìn)一步學(xué)習(xí)javascript數(shù)組,希望大家繼續(xù)關(guān)注2015-11-11微信小程序onLaunch異步,首頁onLoad先執(zhí)行?
這篇文章主要介紹了微信小程序onLaunch異步,首頁onLoad先執(zhí)行? 文章底部給大家介紹了小程序_onLaunch異步回調(diào)數(shù)據(jù)加載問題的兩種解決方案,需要的朋友可以參考下2018-09-09javascript實現(xiàn)在網(wǎng)頁任意處點左鍵彈出隱藏菜單的方法
這篇文章主要介紹了javascript實現(xiàn)在網(wǎng)頁任意處點左鍵彈出隱藏菜單的方法,設(shè)計鼠標(biāo)事件及css樣式操作的相關(guān)技巧,需要的朋友可以參考下2015-05-05