创建对象
对象属性赋值的方式
1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta chaset="UTF-8"> 5 <title></title> 6 </head> 7 <body> 8 <script> 9 people = new Object(); //javascript对象的创建 10 11 //对people对象属性赋值第一种方式 12 people.name = "jerry"; //对people对象的name属性赋值 13 people.age = "18"; //对people对象的age属性赋值 14 document.write("name:"+people.name+",age:"+people.age+"</br>"); 15 16 //对people对象属性赋值第二种方式 17 people = {name:"tom",age:"15"};//对people对象的name属性、age属性赋值 18 document.write("name:"+people.name+",age:"+people.age); 19 20 </script> 21 </body> 22 </html>
创建自定义对象
1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta chaset="UTF-8"> 5 <title></title> 6 </head> 7 <body> 8 <script> 9 //自定义对象的创建 10 //使用函数来创建对象,然后创建对象实例 11 function people(name,age){ 12 this.name = name; 13 this.age = age; 14 } 15 son = new people("tom",30); 16 document.write("name:"+son.name+",age:"+son.age); 17 </script> 18 </body> 19 </html>
javascript内置对象
string对象
1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta chaset="UTF-8"> 5 <title></title> 6 </head> 7 <body> 8 <script> 9 var str = "Hello World"; 10 document.write("字符串长度:"+str.length+"</br>"); 11 document.write("查询字符串的位置:"+str.indexOf("World")+"</br>"); 12 document.write("匹配字符串是否存在,若存在输出这个字符,若不存在输出空:"+str.match("World")+"</br>") 13 document.write("替换字符串:",str.replace("World","javascript")+"</br>"); 14 document.write("字符串转换成大写:"+str.toUpperCase()+"</br>"); 15 document.write("字符串转换成小写:"+str.toLowerCase()+"</br></br>"); 16 17 var str1 = "Hello,tom,jerry,gophi"; 18 document.write("字符串:"+str1+"</br>"); 19 var s = str1.split(","); 20 document.write("字符串分割之后的结果:</br>"); 21 document.write(s[0]+"</br>"); 22 document.write(s[1]+"</br>"); 23 document.write(s[2]+"</br>"); 24 document.write(s[3]+"</br>"); 25 </script> 26 </body> 27 </html>
date对象
1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta chaset="UTF-8"> 5 <title></title> 6 </head> 7 <body> 8 <script> 9 var date = new Date(); 10 document.write("时间:"+date+"</br>"); //获取时间总信息 11 document.write(+date.getFullYear()+"年</br>"); //获取年份 12 document.write("星期"+date.getDay()+"</br>"); //获取星期几 13 document.write("时间戳:"+date.getTime()+"</br>");//获取时间戳 14 date.setFullYear(2000,1,1) //设置时间 15 document.write("设置时间为:"+date+"</br>"); //获取时间总信息 16 </script> 17 </body> 18 </html>
一个简单的时钟程序
1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta chaset="UTF-8"> 5 <title></title> 6 </head> 7 <body onload="startTime()"> <!--用一个onload事件调用startTime函数--> 8 <script> 9 function startTime(){ 10 var today = new Date(); 11 var h = today.getHours(); //获取时 12 var m = today.getMinutes(); //获取分 13 var s = today.getSeconds(); //获取秒 14 m = checkTime(m); 15 s = checkTime(s); 16 17 document.getElementById("timetxt").innerHTML = h+":"+m+":"+s; 18 t = setTimeout(function(){startTime();},1000); //每1000毫秒刷新一次时间 19 //checkTime函数的作用是使分、秒为个位数时仍输出两位,比如9分5秒,输出09:05 20 } 21 function checkTime(i){ 22 if(i<10){ 23 i = "0" + i; 24 } 25 return i; 26 } 27 </script> 28 <div id="timetxt"></div> <!--设置一个显示时间的标签--> 29 </body> 30 </html>
array对象
1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta chaset="UTF-8"> 5 <title></title> 6 </head> 7 <body> 8 <script> 9 //concat():合并数组 10 var a=["hello","world"]; 11 var b=["tom","jerry"]; 12 var c=a.concat(b); 13 document.write(c+"</br>"); 14 15 //sort():排序 16 var aa=["a","e","c","v","b"]; 17 document.write(aa.sort()+"</br>"); 18 19 var bb=[1,9,2,3,4,7,8,5]; 20 document.write(bb.sort()+"</br>"); 21 22 //push():末尾追加元素 23 var cc=["a","b"]; 24 cc.push("c"); 25 document.write(cc+"</br>"); 26 27 //reverse():数组元素翻转 28 var dd=["1","2","3","4","5"]; 29 document.write(dd.reverse()); 30 31 </script> 32 </body> 33 </html>
math对象
1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta chaset="UTF-8"> 5 <title></title> 6 </head> 7 <body> 8 <script> 9 //round(),四舍五入 10 document.write(Math.round(2.5)+"</br>"); 11 //random(),0~1之间的随机数 12 document.write(Math.random()+"</br>"); 13 //parseInt(),取整 14 document.write(parseInt(Math.random()*10)+"</br>");//求0~10之间的随机整数 15 //max(),求最大值 16 document.write(Math.max(10,2000,1,35,6.7,-10)+"</br>"); 17 //min(),求最小值 18 document.write(Math.min(10,2000,1,35,6.7,-10)+"</br>"); 19 //abs(),求绝对值 20 document.write(Math.abs(-10)); 21 </script> 22 </body> 23 </html>