<html> <head> <title>javascript基础</title> </head> <body> 1.流程控制 顺序 选择 循环结构<br/> <script type="text/javascript"> var score = 50; /* 多行注释 if ( score > 60 ) { alert("恭喜,及格了..."); } */ // if ... else ... 单行注释 // if ... else if .... else if .... else ... //switch // ? : document.write("成绩是否合格 : " , score > 60 ? ‘及格‘:‘没通过‘ ,"<br/>"); // while // do while //for( ; ;) break continue 之后的代码不执行 </script> 2.数组<br/> <script type="text/javascript"> var a = new Array();//创建数组 没有长度 没有类型限制 set list document.write("a数组的长度 : " , a.length ,"<br/>"); a[0] = 12; a[1] = 5; a[10] = 35; a[2] = ‘张三‘; a[5] = new Date(); document.write("a数组的长度 : " , a.length ,"<br/>"); document.write("a数组的元素 : " , a.toString() ,"<br/>"); var b = new Array(3); document.write("b数组的长度 : " , b.length ,"<br/>"); b[5] = 100; document.write("b数组的长度 : " , b.length ,"<br/>"); document.write("b数组的元素 : " , b ,"<br/>"); //a = new Array(5); a.length = 5;//修改长度 document.write("a数组的元素 : " , a ,"<br/>"); //初始化 var colors = new Array(‘red‘,‘yellow‘,‘blue‘,‘green‘,‘white‘,‘black‘); colors[6] = "gray"; document.write("colors数组的元素 : " , colors ,"<br/>"); /* for(var i = 0; i < colors.length; i++ ) { document.write(i , " : " , colors[i] ,"<br/>"); } 增强的for循环 for(var i in colors) { document.write( i , " : " , colors[i] , "<br/>"); // i 数组索引 } */ var c = [, new Object , , ‘a‘,new Number(80),"yema"]; c[0] = 20; c[1] = 10; c[2] = 30; document.write("c是否是数组 : " , c instanceof Array , "<br/>"); document.write("c数组的元素 : " , c ,"<br/>"); </script> 3.数组的方法<br/> <script type="text/javascript"> document.write("colors.concat(c) : " , colors.concat(c) ,"<br/>"); document.write("colors.join(‘->‘) : " , colors.join(‘->‘) ,"<br/>"); document.write("colors.pop() : " , colors.pop() ,"<br/>"); colors.push(‘abcd‘); document.write("colors : " , colors ,"<br/>"); document.write("colors.shift() : " , colors.shift() ,"<br/>"); colors.unshift(‘oracle‘); document.write("colors : " , colors ,"<br/>"); document.write("colors.reverse() : " , colors.reverse() ,"<br/>"); document.write("colors.sort() : " , colors.sort() ,"<br/>"); var d = [11,2,6,34] document.write("d.sort() : " , d.sort() ,"<br/>"); document.write("colors.slice(2,5) : " , colors.slice(2,5) ,"<br/>"); document.write("======================== <br/>"); //splice document.write("colors : " , colors ,"<br/>"); colors .splice(1,2);//删除 document.write("colors : " , colors ,"<br/>"); colors.splice(3,0,‘java‘,‘xml‘);//添加 document.write("colors : " , colors ,"<br/>"); colors.splice(1,2,‘football‘,‘swim‘);//修改 document.write("colors : " , colors ,"<br/>"); </script> </body> </html>
rs:
时间: 2024-10-11 22:46:27