標籤:
<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:
javascript 基礎3第13節