javascript中array类型队列方法总结
队列方法
数组推入: push() unshift() 调用方法返回数组新长度
数组移除: pop() shift() 调用方法返回移除的那个元素
注:ie7及更早的版本中,unshift()方法总是返回undefined
代码与演示:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>RunJS</title> </head> <body> <button onclick="javascript:say_hello();">push方法</button> <br> <button onclick="javascript:say_hello2();">unshift方法</button> <br> <button onclick="javascript:say_hello3();">pop方法</button> <br> <button onclick="javascript:say_hello4();">shift方法</button> </body> </html>
js:
function say_hello(){ var colors = ['red','blue','green']; alert(colors.push('white')); //返回长度 alert(colors); } function say_hello2(){ var colors = ['red','blue','green']; alert(colors.unshift('white')); //返回长度 alert(colors); } function say_hello3(){ var colors = ['red','blue','green']; alert(colors.pop('white')); //返回移除的元素 alert(colors); } function say_hello4(){ var colors = ['red','blue','green']; alert(colors.shift('white')); //返回移除的元素 alert(colors); }