数组对象可以像栈一样,也就是一种限制插入和删除项的数据结构。栈是一种后进先出的结构,也就是说最近添加的项先被删除!接下来介绍两种方法:push(),pop()
一、push()方法
push()方法接收任意数量的参数,并将它们添加到数组末尾,返回数组最新长度。
代码如下(示例):
let colors=new Array(); //创建一个数组
let count=colors.push("red","blue"); //推入两个值
alert(count) //2
二、pop()方法
pop()方法删除数组的最后一项,同时减少数组的length值,返回被删除的项。
代码如下(示例):
let colors=new Array(); //创建一个数组
let count=colors.push("red","blue"); //推入两个值
alert(count) //2
let item=colors.pop(); //去掉最后一项
alert(item); //blue