<!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:javascript
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); }