《中级前端1.7》JavaScript内置对象——String,Date,Array,Math
JavaScript一切皆对象,所以也存在很多内置的对象,这些内置对象可以帮我们提供很多方便。
什么是对象
创建自定义对象:
方法1
<!-- 创建对象 --> <script> people = new Object(); people.name = "woodk"; people.age = "26"; document.write("name:" + people.name + ",age:" + people.age); </script>
方法2
<!-- 创建对象 --> <script> people = { name: "woodk", age: "26" }; document.write("name:" + people.name + ",age:" + people.age); </script>
方法3,使用函数来定义对象
<!-- 使用函数来定义并创建对象 --> <script> function people(name, age) { this.name = name; this.age = age; } son = new people("woodk", 26); document.write("name:" + son.name + ",age:" + son.age); </script>
JS内置对象-String字符串对象
<script> var str = "Hello World"; document.write("字符串长度为:" + str.length); document.write(str.indexOf("World")); //匹配到,返回索引位置;匹配不到,返回-1 document.write(str.match("World")); //匹配到,返回World;匹配不到,返回null document.write(str.replace("World", "girl")); </script>
字符串转换为数组:
<script> //较常用,字符串分割 var str1 = "hello,jike,xueyuan"; var s = str1.split(","); document.write(s[1]); </script>
JS内置对象-Date日期对象
常用方法:
<script> var date = new Date(); document.write(date); //Wed Jan 13 2016 03:03:17 GMT+0800 (中国标准时间) document.write(date.getFullYear()); //获取年份 document.write(date.getTime()); //获取时间戳 date.setFullYear(2010, 9, 1); document.write(date); </script>
每秒刷新的时钟:
1 <div id="timtTxt">s</div> 2 <script> 3 startTime(); 4 //时钟 5 function startTime() { 6 var today = new Date(); 7 var h = today.getHours(); 8 var m = today.getMinutes(); 9 var s = today.getSeconds(); 10 m = checkTime(m); 11 s = checkTime(s); 12 document.getElementById("timtTxt").innerHTML = h + ":" + m + ":" + s; 13 t = setTimeout(function() { 14 startTime(); 15 }, 1000); 16 } 17 18 //如果分和秒是个位数时,前面加上一个0 19 function checkTime(i) { 20 if (i < 10) { 21 i = "0" + i; 22 } 23 return i; 24 } 25 </script>
网页显示:
JS内置对象-Array数组对象
<!-- 合并数组 --> <script> var a = ["hello", "world"]; var b = ["woodk", "man"]; var c = a.concat(b); document.write(c); </script>
<!-- 排序 --> <script> var s1 = ["a", "c", "d", "t", "b", "e"]; document.write(s1.sort()); var s2 = [1, 5, 3, 4, 2]; document.write(s2.sort()); document.write(s2.sort(function(a, b) { return a - b; //升序 })); document.write(s2.sort(function(a, b) { return b - a; //降序 })); </script>
<!-- 末尾追加元素 --> <script> var arr1 = ["a", "b"]; arr1.push("ccc"); document.write(arr1); </script>
<!-- 数组元素翻转 --> <script> var arr = ["a", "c", "b"]; document.write(arr.reverse()); </script>
splice()添加,删除数组中元素:
arr.splice(2,0,"William"); //添加 arr.splice(2,1,"William"); //删除并添加 arr.splice(2,3,"William"); //删除3个并添加
JS内置对象-Math对象
<!-- Math数学对象 --> <script> document.write(Math.round(2.5) + "<br>"); //四舍五入 document.write(Math.random() + "<br>"); //随机数从0-1 document.write(parseInt(Math.random() * 10) + "<br>"); //随机一个从0-10的整数 /*每隔1秒输出一个随机数*/ showRandom(); function showRandom() { document.write(parseInt(Math.random() * 10) + ","); //随机一个从0-10的整数 setTimeout(function() { showRandom(); }, 1000); } document.write(Math.max(19, 21, 2, 3)); //最大值 document.write(Math.min(19, 21, 2, 3)); //最小值 document.write(Math.min.apply(null, [19, 21, 2, 3])); //传递数组,获取最小值 document.write(Math.abs(-10)); //绝对值 document.write(Math.floor(5.335)); //输出5; floor获取一个小于或等于目标数字的最接近的整数 </script>