源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

javascript数据类型示例分享

  • 时间:2020-06-20 11:24 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:javascript数据类型示例分享
前面我们介绍了javascript的数据类型,今天我们通过一些例子再来温故一下,希望大家能够达到知新的地步。
[u]复制代码[/u] 代码如下:
<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的数据类型有了新的认识了呢,希望大家能够喜欢。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部