javascript数据类型示例分享


Posted in Javascript onJanuary 19, 2015

前面我们介绍了javascript的数据类型,今天我们通过一些例子再来温故一下,希望大家能够达到知新的地步。

<script type="text/javascript">

        //1、Boolean 类型

        //2、Number 类型

        //3、String 类型

        //Boolean类型容易与基本类型混淆,所以建议永远不要使用Boolean对象。

        //Number是与数字对应的引用类型

        var numberObj = new Number(10);

        //重写toString方法 传入的参数是告诉它放回几进制数字的字符串类型

        var num = 10;

        alert(num.toString());//"10"

        alert(num.toString(2));//"1010"

        alert(num.toString(8));//"12"

        alert(num.toString(10));//"10"

        alert(num.toString(16));//"a"

        //toFixed()方法,是返回指定小数位的数值的字符串表示方法,而且具有四舍五入的功能

        var num = 10;

        num.toFixed(2);//"10.00"

        //toExponential()指数表示法方法,接受一个参数表示输出结果中小数的位数

        var num = 10;

        alert(num.toExponential(1));//"1.0e+1"

        //不过这么小的数字就不需要使用指数表示法了,如果你想得到某个数值最合适的格式就应该使用

        //toPrecision()方法,此方法可能返回固定大小(fixed)格式,也可能返回指数(exponential)格式

        //接受一个参数表示数值所有数字的位数(不包括指数部分)。

        var num = 99;

        alert(num.toPrecision(1));//1e+2,1e+2表示100,因为指数无法表示99所以向上舍入变成100

        alert(num.toPrecision(2));//"99"

        alert(num.toPrecision(3));//"99.0"

        //String对象,String对象的方法也可以在所有的基本字符串中访问到。

        //1、字符操作方法:charAt()、charCodeAt()。每个参数都接受一个基于位置0的字符位置

        var stringValue = "Hello world!";

        stringValue.charAt(1);//"e" 第二个位置是“e”

        stringValue.charCodeAt(1);//"101" 第二个位置“e”的字符编码是“101”

        //2、字符串操作方法concat(拼接的字符)、slice(index,index)、substring(index,index)、substr(index,length)。index:位置,length:长度

        var str1 = "hello";

        alert(str1.concat(" word"));//Hello world

        alert(str1.concat(" word", "!"));//Hello world!

        var stringValue = "Hello world!";

        alert(stringValue.slice(3));//lo world

        alert(stringValue.substring(3));//lo world

        alert(stringValue.substr(3));//lo world

        alert(stringValue.slice(3, 7));//lo w

        alert(stringValue.substring(3, 7));//lo w

        alert(stringValue.substr(3, 7));//lo worl  这个7代表截取的长度

        //3、字符串位置方法 indexOf() 和 lastIndexOf()

        //这两个方法都是从指定的字符串中搜索给定的字符串,然后返回字符串的位置,没有找到就返回-1。

        //这两个方法的区别在于一个是从字符串的开头向后搜索字符串,而lastIndexOf是从字符串的末尾向前搜索字符串。

        //这两个方法都有一个可选的参数(从指定的位置开始搜索)

        var stringValue = "hello word";

        alert(stringValue.indexOf("o"));//4

        alert(stringValue.lastIndexOf("o"));//7

        //可以循环调用indexOf或lastIndexOf来找到指定的字符串

        var stringValue = "wo de wei lai bu shi meng!wo men you geng hao de ming tian!";

        var positions = [];

        var pos = stringValue.indexOf("e");

        while (pos > -1) {

            positions.push(pos);

            pos = stringValue.indexOf("e", pos + 1);

        }

        alert(positions);//4、7、22、33、38、47

        //4、trim()这个方法会创建一个字符串副本,删除前置及后置的所有空格。

        var stringValue="  hello word   ";

        alert(stringValue);

        alert(stringValue.trim());

        //5、字符串大小写转换方法

        //toLowerCase、toLocalLowerCase、toUpperCase、toLocalUpperCase

        var stringValue="hello word";

        alert(stringValue.toLocaleUpperCase());//此方法比较稳妥

        alert(stringValue.toUpperCase());

        alert(stringValue.toLocaleLowerCase());//此方法比较稳妥

        alert(stringValue.toLowerCase());

        //6、字符串匹配方法 replace()

        //这个方法接受两个参数,第一个参数是一个正则表达式或者字符串,第二个参数是一个字符串或一个函数

        var text="cat,bat,sat,fat";

        var result=text.replace("at","ond");//

        alert(result);//"cond,bond,sond,fond"

        var result=text.replace(/at/g,"ond");//

        alert(result);//"cond,bond,sond,fond"

        var text="cat,bat,sat,fat";

        result=text.replace(/(.at)/g,"word ($1)");

        alert(result);

        //replace的第二个参数也可以是一个函数

        function htmlEscape(text) {

            //函数有是三个参数:1、模式匹配项 2、模式匹配项在字符中的位置 3、原始字符串

            return text.replace(/[<>"&]/g,function(match,index,text){

                switch (match){

                    case "<":

                         return "<";

                    case ">":

                        return ">";

                    case "&":

                        return "&";

                    case "\"":

                        return """;

                }

            });

        }

        alert(htmlEscape("<p class=\"greeting\">Hello World!</p>"));

        //<p class="greeting">Hello World!</p>

        //localCompare()比较两个字符串。A.localCompare("B")

        //如果字符串(A)在字母表中排在字符串参数(B)之前,这返回负数(-1)

        //如果字符串等于字符串参数则返回0

        //如果字符串(A)在字母表中排在字符串参数(B)之后则返回正数(1)

        var stringValue="f";

        alert(stringValue.localeCompare("d"));//1

        alert(stringValue.localeCompare("f"));//0

        alert(stringValue.localeCompare("z"));//-1

        //fromCharCode 这个静态方法是与charCodeAt执行相反的操作

        alert(String.fromCharCode(104,101,108,108,111));//"hello"

        //7、html方法建议不要使用。

    </script>

END

童鞋们是否对javascript的数据类型有了新的认识了呢,希望大家能够喜欢。

Javascript 相关文章推荐
javascript 中对象的继承〔转贴〕
Jan 22 Javascript
JavaScript Archive Network 集合
May 12 Javascript
qTip 基于JQuery的Tooltip插件[兼容性好]
Sep 01 Javascript
超级简单的jquery操作表格方法
Dec 15 Javascript
Javascript 正则表达式实现为数字添加千位分隔符
Mar 10 Javascript
jQuery实现购物车计算价格功能的方法
Mar 25 Javascript
JS实现DIV高度自适应窗口示例
Feb 16 Javascript
Vue组件化开发思考
Feb 02 Javascript
微信小程序控制台提示warning:Now you can provide attr &quot;wx:key&quot; for a &quot;wx:for&quot; to improve performance解决方法
Feb 21 Javascript
详解写好JS条件语句的5条守则
Feb 28 Javascript
浅谈微信小程序列表埋点曝光指南
Oct 15 Javascript
如何手写简易的 Vue Router
Oct 10 Javascript
jQuery中outerHeight()方法用法实例
Jan 19 #Javascript
jQuery中innerWidth()方法用法实例
Jan 19 #Javascript
JS实现页面超时后自动跳转到登陆页面
Jan 19 #Javascript
jQuery中innerHeight()方法用法实例
Jan 19 #Javascript
Javascript基础教程之函数对象和属性
Jan 18 #Javascript
Javascript基础教程之argument 详解
Jan 18 #Javascript
Javascript基础教程之定义和调用函数
Jan 18 #Javascript
You might like
ThinkPHP的cookie和session冲突造成Cookie不能使用的解决方法
2014/07/01 PHP
php文件压缩之PHPZip类用法实例
2015/06/18 PHP
php实现对象克隆的方法
2015/06/20 PHP
CI框架使用composer安装的依赖包步骤与方法分析
2016/11/21 PHP
PHP解决中文乱码
2017/04/28 PHP
Jquery中获取iframe的代码
2011/01/11 Javascript
JavaScript去除空格的三种方法(正则/传参函数/trim)
2013/02/06 Javascript
jquery如何根据值设置默认的选中项
2014/03/17 Javascript
深入理解JavaScript函数参数(推荐)
2016/07/26 Javascript
Bootstrap面板(Panels)的简单实现代码
2017/03/17 Javascript
开发Vue树形组件的示例代码
2017/12/21 Javascript
vue自定义全局组件(自定义插件)的用法
2018/01/30 Javascript
vue 实现LED数字时钟效果(开箱即用)
2019/12/08 Javascript
[02:53]2018年度DOTA2最佳战队-完美盛典
2018/12/17 DOTA
Python 命令行参数sys.argv
2008/09/06 Python
python实现上传样本到virustotal并查询扫描信息的方法
2014/10/05 Python
举例讲解Python中的Null模式与桥接模式编程
2016/02/02 Python
python递归函数绘制分形树的方法
2018/06/22 Python
python实现简单名片管理系统
2018/11/30 Python
python sort、sort_index方法代码实例
2019/03/28 Python
django框架模板语言使用方法详解
2019/07/18 Python
Python3.7实现验证码登录方式代码实例
2020/02/14 Python
new_zeros() pytorch版本的转换方式
2020/02/18 Python
Pytorch 使用CNN图像分类的实现
2020/06/16 Python
python创建文本文件的简单方法
2020/08/30 Python
物业门卫岗位职责
2013/12/28 职场文书
长安大学毕业生自我鉴定
2014/01/17 职场文书
2014新年寄语
2014/01/20 职场文书
数据保密承诺书
2014/06/03 职场文书
公司给客户的感谢信
2015/01/23 职场文书
大学军训决心书
2015/02/05 职场文书
2015年大学宣传部工作总结
2015/05/26 职场文书
MySQL kill不掉线程的原因
2021/05/07 MySQL
MySQL系列之十一 日志记录
2021/07/02 MySQL
Python类方法总结讲解
2021/07/26 Python
GPU服务器的多用户配置方法
2022/07/07 Servers