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>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM