typedArray.copyWithin()是JavaScript中的內置函數,用於在同一typedArray中的指定位置複製typedArray的某些元素。
用法:
typedArray.copyWithin(target, start, end)
參數:它接受下麵指定的三個參數-
- target:它是從中複製元素的起始索引位置。
- start:它是從其開始複製元素的起始索引位置,其默認值為typedArray的起始索引。
- end:它是可選的,它是要複製的元素之前的結束位置索引,並且其默認值是typedArray的結束。
返回值:複製過程完成後,它將返回修改後的數組。
代碼1:
<script>
// Constructing a new typedArray "A"
// with some elements
const A = new Uint8Array([ 5, 10, 15, 20, 25, 30, 35, 40 ]);
// Calling copyWithin function to copy
// element from index position 0 and
// element from index 4 to 5
A.copyWithin(0, 4, 5);
// Printing a new modified array
document.write(A);
</script>
輸出:
25,10,15,20,25,30,35,40
代碼2:
<script>
// Constructing some new typedArrays
// with some elements
const A = new Uint8Array([ 5, 10, 15, 20, 25, 30, 35, 40 ]);
const B = new Uint8Array([ 5, 10, 15, 20, 25, 30, 35, 40 ]);
const C = new Uint8Array([ 5, 10, 15, 20, 25, 30, 35, 40 ]);
const D = new Uint8Array([ 5, 10, 15, 20, 25, 30, 35, 40 ]);
const E = new Uint8Array([ 5, 10, 15, 20, 25, 30, 35, 40 ]);
// Calling copyWithin function with different
// parameters
a = A.copyWithin(0, 5);
b = B.copyWithin(1, 4);
c = C.copyWithin(0, 4, 5);
d = D.copyWithin(2, 3, 5);
e = E.copyWithin(0, 1, 4);
// Printing new modified arrays
document.write(a +"<br>");
document.write(b +"<br>");
document.write(c +"<br>");
document.write(d +"<br>");
document.write(e);
</script>
輸出:
30,35,40,20,25,30,35,40 5,25,30,35,40,30,35,40 25,10,15,20,25,30,35,40 5,10,20,25,25,30,35,40 10,15,20,20,25,30,35,40
相關用法
- Javascript typedArray.from()用法及代碼示例
- Javascript typedArray.of()用法及代碼示例
- Javascript weakSet.has()用法及代碼示例
- Javascript typedArray.map()用法及代碼示例
- Javascript weakSet.add()用法及代碼示例
- Javascript weakMap.set()用法及代碼示例
- Javascript weakMap.has()用法及代碼示例
- Javascript typedArray.every()用法及代碼示例
- Javascript getPrototypeOf()用法及代碼示例
- Javascript uneval()用法及代碼示例
- Javascript parseInt()用法及代碼示例
- Javascript parseFloat()用法及代碼示例
注:本文由純淨天空篩選整理自ShivamKD大神的英文原創作品 JavaScript | typedArray.copyWithin() with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。