标签:
一、js的String对象
<script type="text/javascript"> var a = "drby"; document.write(a.length); //4 </script>
<script type="text/javascript"> var str="Hello world!" document.write(str.bold()) //helloworld!显示为粗体 【发现语句结束不加分号也能运行成功QAQ】 </script>
<script type="text/javascript"> var str="Hello world!" document.write(str.fontcolor("Red")) </script>
<script type="text/javascript"> var str="Hello world!" document.write(str.fontsize(7)) </script>
<script type="text/javascript"> var str="this is a url" document.write(str.link("http://www.w3school.com.cn")) </script>
<script type="text/javascript"> var str="Hello world!下标" document.write(str.sub()) var str1="Hello world!上标" document.write(str1.sup()) </script>
<script type="text/javascript"> //concat方法 var str1 = "abc"; var str2 = "dfg"; document.write(str1.concat(str2)); document.write("<hr>"); //charAt方法 var str3 = "abcdefg"; document.write(str3.charAt(20)); //字符位置不存在,返回空字符串 document.write(str3.charAt(3)); document.write("<hr>"); //indexOf方法 var str4 = "poiuyt"; document.write(str4.indexOf("y")); document.write(str4.indexOf("w")); //字符不存在,返回-1 document.write("<hr>"); //split方法 【更多例子用法查看w3c文档】 var str5 = "a-b-c-d"; var arr1 = str5.split("-"); document.write("length: "+arr1.length + "<br />"); var str="How are you doing today?" document.write(str.split(" ") + "<br />") document.write(str.split("") + "<br />") document.write(str.split(" ",3)) document.write("<hr>"); //replace方法 【更多例子用法查看w3c文档】 var str6 = "abcd"; document.write(str6); document.write("<br/>"); document.write(str6.replace("a","Q")); //次处传两个参数,第一个原始字符,第二个是要替换成的字符 document.write("<hr>"); //substr方法 var str7 = "abcdefghuiop"; document.write(str7.substr(5,5)); //fghui 从第五位开始,向后截取几位 document.write("<hr>"); //substring方法 var str8 = "abcdefghuiopqrst"; document.write(str7.substring(3,5)); //de 从第几位开始到第几位结束 [3,5) 包左不包右 </script>
需要注意的是,JavaScript 的字符串是不可变的(immutable),String 类定义的方法都不能改变字符串的内容。像 String.toUpperCase() 这样的方法,返回的是全新的字符串,而不是修改原始字符串。
二、js的Array对象
<script type="text/javascript"> var arr = new Array(3) arr[0] = "John" arr[1] = "Andy" arr[2] = "Wendy" document.write("Original length: " + arr.length) document.write("<br />") arr.length=5 document.write("New length: " + arr.length) </script> //结果: //Original length: 3 //New length: 5
<script type="text/javascript"> //concat方法 var arr1 = [1,2,3]; var arr2 = [4,5,6]; var arr3 = [7,8,9]; document.write(arr1.concat(arr2) +"<br/>"); //1,2,3,4,5,6 document.write(arr1.concat(arr2,arr3)); //1,2,3,4,5,6,7,8,9 document.write("<hr>"); //join方法 var arr4 = new Array(3) arr4[0] = "George" arr4[1] = "John" arr4[2] = "Thomas" document.write(arr4.join() +"<br/>") //George,John,Thomas document.write(arr4.join(".")) //George.John.Thomas document.write("<hr>"); //push方法 var arr5 = new Array(3); arr5[0] = "tom"; arr5[1] = "lucy"; arr5[2] = "jack"; document.write("old array: "+arr5); document.write("<br/>"); document.write("old length:"+arr5.length); document.write("<br/>"); document.write("new length: "+arr5.push("zhangsan")); document.write("<br/>"); document.write("new array: "+arr5); document.write("<br/>","<br/>"); var arr6 = ["www","qqq"]; var arr7 = ["aaa","bbb","ccc"]; document.write("old array:"+arr6 +"<br/>"); document.write("old length:"+arr6.length +"<br/>"); document.write("new length:"+arr6.push(arr7) +"<br/>"); //结果3 不是5 ,因为把arr7当做一个整体字符串(即一个元素)添加到arr6 document.write("new array: "+arr6); for(var i=0;i<arr6.length;i++) { alert(arr6[i]); } document.write("<hr>"); //pop方法 var arr8 = ["zhangsan","lisi","wangwu","zhaoliu"]; document.write("old array: "+arr8 +"<br/>"); document.write("return: "+arr8.pop() +"<br/>"); document.write("new array: "+arr8); document.write("<hr>"); //reverser方法 var arr9 = ["zhangsan1","lisi1","zhaoliu1","niuqi1"]; document.write("old array: "+arr9); document.write("<br/>"); document.write("new array:"+arr9.reverse()); </script>
三、js的Date对象
Date date = new Date(); //格式化 toLocaleString() //xxxx年xx月xx日 xx:xx:xx
<script type="text/javascript"> //获取当前时间 var date = new Date(); document.write(date); // Sat May 28 2016 22:33:17 GMT+0800 (中国标准时间) //转换成习惯的格式 document.write("<hr/>"); document.write(date.toLocaleString()); //2016年5月28日 22:33:17 </script>
var date = new Date(); document.write("year: "+date.getFullYear()); //year: 2016
<script type="text/javascript"> //返回的是 0-11月,如果想要得到准确的值,加1 var date = new Date(); var date1 = date.getMonth()+1; document.write("month: "+date1); </script>
<script type="text/javascript"> //返回的是 (0 ~ 6) //外国朋友,把星期日作为一周的第一天,星期日返回的是0,而星期一到星期六 返回的是 1-6 var date = new Date(); document.write("week: "+date.getDay()); </script>
<script type="text/javascript"> //得到当前的天 1-31 var date = new Date(); document.write("day: "+date.getDate()); </script>
<script type="text/javascript"> //得到当前的小时 0~23 var date = new Date(); document.write("hour: "+date.getHours()); </script>
<script type="text/javascript"> var date = new Date(); document.write("minute: "+date.getMinutes()); </script>
var date = new Date(); document.write("second: "+date.getSeconds());
var date = new Date(); document.write("second: "+date.getTime());
常用在防止浏览器缓存时候 ,比如银行网站就不需要缓存
使用毫秒数处理缓存的效果(不有缓存) http://www.baidu.com?毫秒数四、js的Math对象
<script type="text/javascript"> var mm = 10.3; //不用创建math对象 document.write(Math.ceil(mm)); //11 </script>
Math.random()*10
Math.floor(Math.random()*10)); //Math.floor舍去小数部分
五、js的全局函数
六、js的函数的重载
标签:
原文地址:http://www.cnblogs.com/drby/p/5538674.html