首页 > 网站 > WEB开发 > 正文

javascript unshift()和shift()

2024-04-27 14:07:40
字体:
来源:转载
供稿:网友

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>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表