此函數將數組的一部分淺複製到同一數組中的另一個位置,並在不修改其長度的情況下返回它。
用法
下麵陳述的語法用於數組方法“.copyWithin()”,其中,
target- 將序列複製到的從零開始的索引。如果為負,則目標將從末尾開始計數。
start- 這是一個可選參數。從零開始複製元素的索引。如果為負數,start 將從末尾開始計數。如果省略開始,copyWithin將從索引 0 複製。
end- 這是一個可選參數。從零開始的索引,從中結束複製元素。copyWithin複製到但不包括結束。如果為負,則從末尾開始計數。如果省略結束,copyWithin將複製到最後一個索引。
arr.copyWithin(target[, start[, end]])
示例
<script>
//copy with in
let marks = [10,20,30,40,50,60]
console.log(marks.copyWithin(0,2,4)) //destination,source start,source end(excluding)
console.log(marks.copyWithin(2,4))//destination,source start,(till length)
</script>
上述代碼的輸出將如下所示 -
[30, 40, 30, 40, 50, 60] [30, 40, 50, 60, 50, 60]
注:本文由純淨天空篩選整理自 ES6 - Math.trunc()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。