標籤:
<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()