一张草稿纸

导航

数组

 数组的定义:字面量方式和构造函数方式。

 数组转换:toString方法、valueof方法、toLocaleString方法、join方法,默认输出数组时,默认隐式调用toString、valueof 

 数组操作元素方法:splice方法是数组中最强大的方法,用法有三种:

    第一,用于删除元素,给方法传两个参数:要删除元素开始位置和要删除的元素项数。

           第二,用于向指定的位置插入任意数量数组项,给方法传三个参数:起始位置、0和要插入的项。

           第三,用于替换,给方法传三个参数:起始位置、要删除的项数和要插入的任意数量的项。 

数组队列方法(先进先出):第一种理解:push从后面插入、shift从顶部移除,第二种理解:pop从后面移除、unshift从顶部插入

 数组栈方法(后进先出):push推入、pop弹出,只操作栈顶,可以理解为只操作数组的尾部。

实例代码: 

<html>
	<head>
	</head>
	<body>
		<script type="text/javascript">
				var colors = ["red","red","blue","blue","blue","white","black","blue"];
				//新增数组项,从8-99全都为undefined
				colors[8]="gray";
				document.write("colors[8]="+ colors[8]+"<br>");
				document.write("colors[99]="+ colors[99]+"<br>");
				//判断是否为undefined
				if(colors[99]==undefined){
					alert(true);
					}
				colors[7]="grey";
				//toString
				document.write("toString:"+ colors.toString()+"<br>");
				//join
				//alert(colors.join('|'));
				document.write("join:"+colors.join('|')+"<br>");
				//toLocaleString
				document.write("toLocaleString:"+ colors.toLocaleString()+"<br>");
				//循环遍历数组用in
				
				for(var s in colors)
					{
						//alert(s);
					  document.write(s+":"+colors[s]+"<br>");
					}
				
				document.write("数组总长度:"+colors.length);
				document.write("<br>");
				colors.push("red");
				document.write("push后数组总长度:"+colors.length);
				document.write("<br>");
				document.write("shift取一个元素:"+colors.shift());
				document.write("<br>");
				document.write("shift后数组:"+colors);
				document.write("<br>");
				colors.unshift("black");
				document.write("unshift后数组:"+colors);
				colors.pop();
				document.write("<br>");
				document.write("pop后数组:"+colors);
			</script>
	</body>
</html>

posted on 2011-09-29 22:25  start.sh  阅读(159)  评论(0)    收藏  举报