目录:

1 : 伪对象 
2 : 转换为字符串 
3 : 数字转字符串 
4 : 转换为数字 
5 : 转换为Boolean 
6 : Number()和parseInt()的区别 
7 : String()和toString()的区别

1 : 伪对象

伪对象:javascript是一门很有意思的语言,即便是基本类型,也是伪对象,所以他们都有属性和方法。

变量a的类型是字符串,通过调用其为伪对象的属性length获取其长度 。

<script>
 var a="hello javascript"; 
 document.write("变量a的类型是:"+(typeof a));
 document.write("<br>");
 document.write("变量a的长度是:"+a.length);
</script>

运行效果:
变量a的类型是:string
变量a的长度是:16

2 : 转换为字符串

无论是Number,Boolean还是String都有一个toString方法,用于转换为字符串

<script>
 var a=10; 
 document.write("数字 "+a+" 转换为字符串"+a.toString());
 document.write("<br>");

 var b=true; 
 document.write("布尔 "+b+" 转换为字符串"+b.toString());
 document.write("<br>");

 var c="hello javascript"; 
 document.write("字符串 "+c+" 转换为字符串 "+c.toString());
 document.write("<br>");

</script>

运行效果:
数字 10 转换为字符串10
布尔 true 转换为字符串true
字符串 hello javascript 转换为字符串 hello javascript

3 : 数字转字符串

Number转换为字符串的时候有默认模式和基模式两种

<script>
 var a=10; 
 document.write('默认模式下,数字10转换为十进制的'+a.toString()); //默认模式,即十进制
 document.write("<br>"); 

 document.write('基模式下,数字10转换为二进制的'+a.toString(2)); //基模式,二进制
 document.write("<br>"); 
 
 document.write('基模式下,数字10转换为八进制的'+a.toString(8)); //基模式,八进制
 document.write("<br>"); 

 document.write('基模式下,数字10转换为十六进制的'+a.toString(16)); //基模式,十六进制
 document.write("<br>"); 

</script>

运行效果:
默认模式下,数字10转换为十进制的10
基模式下,数字10转换为二进制的1010
基模式下,数字10转换为八进制的12
基模式下,数字10转换为十六进制的a

4 : 转换为数字

javascript分别提供内置函数 parseInt()和parseFloat(),转换为数字

注:如果被转换的字符串,同时又数字和字符构成,那么parseInt会一直定位数字,直到出现非字符。 所以"10abc" 会被转换为 10

思考题: 字符串"10abc8" 又会被转换为多少呢?

<script>
 document.write("字符串的\"10\"转换为数字的:"+parseInt("10")); //转换整数
 document.write("<br>");
 document.write("字符串的\"3.14\"转换为数字的:"+parseFloat("444 3.14"));//转换浮点数
 document.write("<br>");
 document.write("字符串的\"10abc\"转换为数字的:"+parseInt("10abc")); //判断每一位,直到发现不是数字的那一位
 document.write("<br>");

 document.write("字符串的\"hello javascript\"转换为数字的:"+parseInt("h5555ello javascript")); //如果完全不包含数字,则返

回NaN - Not a Number
 document.write("<br>");

</script>

运行效果:
字符串的"10"转换为数字的:10
字符串的"3.14"转换为数字的:444
字符串的"10abc"转换为数字的:10
字符串的"hello javascript"转换为数字的:NaN

5 : 转换为Boolean

使用内置函数Boolean() 转换为Boolean值
当转换字符串时:
非空即为true
当转换数字时:
非0即为true
当转换对象时:
非null即为true

<script>
 document.write("空字符串''转换为布尔后的值:"+Boolean("")); //空字符串
 document.write("<br>");
 document.write("非空字符'hello javascript '串转换为布尔后的值:"+Boolean("hello javascript")); //非空字符串
 document.write("<br>");
 document.write("数字 0 转换为布尔后的值:"+Boolean(0)); //0
 document.write("<br>");
 document.write("数字 3.14 转换为布尔后的值:"+Boolean(3.14)); //非0 
 document.write("<br>");
 document.write("空对象 null 转换为布尔后的值:"+Boolean(null)); //null
 document.write("<br>");
 document.write("非对象 new Object() 转换为布尔后的值:"+Boolean(new Object())); //对象存在
 document.write("<br>");
</script>

运行效果:
空字符串''转换为布尔后的值:false
非空字符'hello javascript '串转换为布尔后的值:true
数字 0 转换为布尔后的值:false
数字 3.14 转换为布尔后的值:true
空对象 null 转换为布尔后的值:false
非对象 new Object() 转换为布尔后的值:true

6 : Number()和parseInt()的区别 

Number()和parseInt()一样,都可以用来进行数字的转换

区别在于,当转换的内容包含非数字的时候,Number() 会返回NaN(Not a Number)

parseInt() 要看情况,如果以数字开头,就会返回开头的合法数字部分,如果以非数字开头,则返回NaN

<script>
 document.write("通过Number() 函数转换字符串'123' 后得到的数字:"+Number("123"));  //正常的
 document.write("<br>");
 document.write("通过Number() 函数转换字符串'123abc' 后得到的数字:"+Number("123abc"));  //包含非数字
 document.write("<br>");
 document.write("通过Number() 函数转换字符串'abc123' 后得到的数字:"+Number("abc123"));  //包含非数字
 document.write("<br>");

 document.write("通过parseInt() 函数转换字符串'123' 后得到的数字:"+parseInt("123"));  //正常的
 document.write("<br>");
 document.write("通过parseInt() 函数转换字符串'123abc' 后得到的数字:"+parseInt("123abc"));  //包含非数字,返回开头的合法

数字部分
 document.write("<br>");
 document.write("通过parseInt() 函数转换字符串'abc123' 后得到的数字:"+parseInt("abc123"));  //包含非数字,以非数字开头,

返回NaN
 document.write("<br>");

</script>

运行效果:

通过Number() 函数转换字符串'123' 后得到的数字:123
通过Number() 函数转换字符串'123abc' 后得到的数字:NaN
通过Number() 函数转换字符串'abc123' 后得到的数字:NaN
通过parseInt() 函数转换字符串'123' 后得到的数字:123
通过parseInt() 函数转换字符串'123abc' 后得到的数字:123
通过parseInt() 函数转换字符串'abc123' 后得到的数字:NaN

7 : String()和toString()的区别

String()和toString()一样都会返回字符串,区别在于对null的处理
String()会返回字符串"null"
toString() 就会报错,无法执行

<script>
 var a = null;
 document.write('String(null) 把空对象转换为字符串:'+String(a)); 
 document.write("<br>"); 
 document.write('null.toString() 就会报错,所以后面的代码不能执行'); 
 document.write(a.toString()); 
 document.write("因为第5行报错,所以这一段文字不会显示"); 
</script>

运行效果:
String(null) 把空对象转换为字符串:null
null.toString() 就会报错,所以后面的代码不能执行

以上就是小编为大家带来的老生常谈javascript的类型转换全部内容了,希望大家多多支持~

标签:
javascript的类型转换

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
评论“老生常谈javascript的类型转换”
暂无“老生常谈javascript的类型转换”评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。