• javascript基础:数据类型的转换


    1、数据类型的强制转换

    包括:

    数字+字符串:数字被强转成字符串。

    布尔+字符串:布尔被强制转为字符串。

    布尔+数字:布尔强转为数字。

    <!doctype html>
    <html>
     <head>
    	<meta charset="gb2312">
    	<title>实验</title>
    	</script>	
     </head>
    
     <body>
        <script language="javascript">
    		var str,i,b,bb,u;
    
    		str ="123";
    		i = 1;
    		b = true;
    		bb = false;
    		u = null;
    
    		document.write("<br>i+str(数字+字符串):" + (i+str));
    
    		document.write("<br>b+str(布尔+字符串):" + (b+str));
    
    		document.write("<br>b+i(布尔+数字):" + (b+i));
    
    		document.write("<br>bb+i(布尔+数字):" + (bb+i));
    
    		document.write("<br>u+str(null+字符串):" + (u+str));		
    	</script>
     </body>
    </html>



    2、数据类型的转换 函数

    (1)parseInt:把字符串变量开头的数字,按照指定的进制(就是我们认为数字的进制,不是要转化成为什么进制),转成10进制的整数。

    比如,下面代码中最后一个16fe,我们认为这个是8进制的数字,因为在8进制里根本没有fe这种只有在16进制才有的符号,所以把8进制数字16,转成10进制的数字,也就是1*8+6 = 14.

    还有第3个xx 10,因为开头的字符里没有数字,所以返回结果就是NaN, 也就是说不是一个数字,not a number。

    <!doctype html>
    <html>
     <head>
    	<meta charset="gb2312">
    	<title>实验</title>
    	</script>	
     </head>
    
     <body>
        <script language="javascript">
    
    		console.log(parseInt("10 xx"));
    		console.log(parseInt("10.6"));
    		console.log(parseInt("xx 10"));
    		console.log(parseInt("16fe的值",16));
    		console.log(parseInt("16fe的值",10));
    		console.log(parseInt("16fe的值",8));
    
    	</script>
     </body>
    </html>


    (2)parseFloat:把字符串开头的浮点数转成浮点数。

    和上面的parseInt类似。

    (3)typeof:返回变量的数据类型。

    注意,如果变量值为null,那么返回的类型是object,而不是null类型。

    <!doctype html>
    <html>
     <head>
    	<meta charset="gb2312">
    	<title>实验</title>
    	</script>	
     </head>
    
     <body>
        <script language="javascript">
            var s = null;
    		var ss;
    		var b = false;
    
    		console.log(typeof(s));
    		console.log(typeof(ss));
    		console.log(typeof(b));
    
    		console.log(typeof("xxx 10"));
    		console.log(typeof(10.5));
    
    	</script>
     </body>
    </html>


    (4)eval:把字符串中参数作为表达式,返回这个表达式的结果。

    <!doctype html>
    <html>
     <head>
    	<meta charset="gb2312">
    	<title>实验</title>
    	</script>	
     </head>
    
     <body>
        <script language="javascript">
            var s = 5;
    		var ss = 6;
    
    		console.log(eval(s+ss));
    		console.log(eval("5*6+8+s+ss"));
    		console.log(eval("5*6+'100'"));
    	</script>
     </body>
    </html>

    从输出结果可以看出,eval的功能非常强大,除了可以计算:基本的变量相加的表达式,还可以嵌入变量,引入字符串后,还可以实现数字+字符串的 计算,不过结果就是个字符串了。

    之前在sql server板块,经常有人问如何实现eval的功能,实际上sql语句更加倾向于一般的求和、计数等,不适合用来做解析字符串表达式,每一种语言都有适用范围。



  • 相关阅读:
    【Linux】ubuntu各文件夹简介
    【Linux】 ubuntu 12.04 iNode Client找不到库libjpeg和libtiff的解决方法
    【Coding】ant 的 javac标签 (归纳)
    【Coding】Ant脚本命令
    【Linux】Ubuntu使用技巧
    【Linux】ubuntu下词典软件Goldendict介绍(可屏幕取词)和StarDict(星际译王)的安装
    【Coding】Ubuntu/环境变量:修改/etc/environment 导致开机不能登录!
    备用访问映射
    开发Silverlight类型的WebPart部署到Sharepoint2010上(转)
    (转)通过Internet访问 SharePoint
  • 原文地址:https://www.cnblogs.com/momogua/p/8304363.html
Copyright © 2020-2023  润新知