码迷,mamicode.com
首页 > 编程语言 > 详细

javascript unshift()和shift()

时间:2015-06-30 21:40:11      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

<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>

javascript unshift()和shift()

标签:

原文地址:http://www.cnblogs.com/qq-757617012/p/4611525.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!