javascript unshift()和shift()

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<script type="text/javascript">
console.log('unshift()和shift()方法的行为非常类似于push()和pop(),不一样的是前者在数组的头部而非尾部进行元素的插入和删除操作。');
console.log('unshift() 在数组的头部添加一个或多个元素,并将已存在的元素移动到更高索引的位置来获得足够的空间,最后返回数组新的长度。');
console.log('shift()删除数组的第一个元素并将其返回,然后把所有随后的元素下移一个位置来填补数组头部的空缺。');


var a=[];
var ret=a.unshift(1);
console.log('01. a.unshift(1)   a='+a+' : ret='+ret);
ret =a.unshift(22);
console.log('02. a.unshift(22)  a='+a+' : ret='+ret);
a.shift();
console.log('03. a.shift()  a='+a+' : ret='+ret);
a.unshift(3,[4,5]);
console.log('04. a.unshift(3,[4,5])  a='+a+' : ret='+ret);
a.shift();
console.log('05. a.shift()  a='+a+' : ret='+ret);
a.shift();
console.log('06. a.shift()  a='+a+' : ret='+ret);
a.shift();
console.log('07. a.shift()  a='+a+' : ret='+ret);

</script>
</body>
</html>
posted @ 2015-06-30 20:36  刘江龙  阅读(2457)  评论(0编辑  收藏  举报