<script type="text/javascript">
var newObject=new Array("red","green","blue");
document.write(newObject); //output red,green,blue
//Array拥有栈结构的特征
newObject.pop();
document.write("<br>"+newObject); //output red,green
newObject.push("new color");
document.write("<br>"+newObject); //output red,green,new color
/*test the method shift() and unshift();
shift()删除数组的第一项,并返回删除的元素值;由shift()和push()方法可以构成类似队列结构的功能。
unshift()添加某个元素到数组的第一项,其他原本元素向下推移。
*/
var test=newObject.shift();
document.write("<br>"+newObject); //output green,new color
newObject.unshift("pink");
document.write("<br>"+newObject); //output pink,green,new color
</script>
javascript Array类 学习
最新推荐文章于 2025-06-17 20:15:16 发布
本文通过一个具体的JavaScript示例,详细介绍了如何使用数组的方法进行元素的添加、删除等操作,包括pop()、push()、shift()和unshift()等常用方法,并展示了这些方法如何帮助实现栈和队列的数据结构。
1433

被折叠的 条评论
为什么被折叠?



