亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我發現concat與unshift有一些微妙的差異,不知道為什么... ...

我發現concat與unshift有一些微妙的差異,不知道為什么... ...

倚天杖 2023-02-11 17:13:25
1)下面第二行,需要對hello進行重新賦值,不然第三行document.write的結果會是abcde沒有f,g也沒有x和y,請問這是為什么?var hello=['a','b','c','d','e','f','g'];hello=hello.concat(['x','y']);document.write(hello);2)下面第二行如果對hello重新賦值的話,document.write結果不是0abcdefg,而是8,請問這是為什么?var hello=['a','b','c','d','e','f','g'];hello = hello.unshift('0');document.write(hello);3)unshift和concat都是控制數列的固有函數,但為什么在這里會有差異?
查看完整描述

2 回答

?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

<script type="text/javascript">
var arr=['a','b','c','d','e','f','g'];
arr.concat(['x','y']);
alert(arr.concat(['x','y']));
alert(typeof(arr.concat(['x','y'])));
document.writeln(arr);
document.write(arr.concat('x','y'));
</script>

<script type="text/javascript">
var arr=['a','b','c','d','e','f','g'];
arr.unshift('0');
alert(arr);
alert(typeof(arr.unshift('0')));
document.writeln(arr);
document.writeln(arr.unshift('0'));
</script>

我自己做了實驗,總結出你自己沒有好好區分數組和數組.方法()。第一個concat,也不需要對hellow從新賦值,你要么直接輸出arr.concat('x','y').要么重新定義一個數組來存放結果。
第二個arr是數組,arr.unshift('0')是數組方法返回的是個number。

 


查看完整回答
反對 回復 2023-02-15
?
小唯快跑啊

TA貢獻1863條經驗 獲得超2個贊

  1. var hello=['a','b','c','d','e','f','g'];

    > undefined

    hello.concat(['x','y']);

    > ["a", "b", "c", "d", "e", "f", "g", "x", "y"]

    hello

    > ["a", "b", "c", "d", "e", "f", "g"]

  2. 綜上:concat 是連接之后的新的數組,但是不會修改原始數組,

    原始數組不發生變化。這個是我在瀏覽器控制臺打印的信息,和你的表述不同

  3. unshift返回的是被修改后的數組的長度,所以是8


查看完整回答
反對 回復 2023-02-15
  • 2 回答
  • 0 關注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號