ฟังก์ชัน copyWithin() ของวัตถุ TypedArray จะคัดลอกเนื้อหาของ TypedArray นี้ภายในตัวมันเอง วิธีนี้ยอมรับตัวเลขสามตัวโดยที่หมายเลขแรกแสดงถึงดัชนีของอาร์เรย์ที่ควรเริ่มต้นการคัดลอกองค์ประกอบ และตัวเลขสองตัวถัดไปแสดงถึงองค์ประกอบเริ่มต้นและสิ้นสุดของอาร์เรย์ซึ่งข้อมูลควรถูกคัดลอก (นำมา)พี>
ไวยากรณ์
ไวยากรณ์ของมันคือดังต่อไปนี้
obj.copyWithin(3, 1, 3);
ตัวอย่าง
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<script type="text/javascript">
var int32View = new Int32Array([21, 64, 89, 65, 33, 66, 87, 55 ]);
document.write("Contents of the typed array: "+int32View);
int32View.copyWithin(5, 0, 5);
document.write("<br>");
document.write("Contents of the typed array after copy: "+int32View);
</script>
</body>
</html> ผลลัพธ์
Contents of the typed array: 21,64,89,65,33,66,87,55 Contents of the typed array after copy: 21,64,89,65,33,21,64,89
ตัวอย่าง
ไม่จำเป็นต้องส่งพารามิเตอร์ตัวที่สามไปยังฟังก์ชันนี้ (องค์ประกอบสิ้นสุดของอาร์เรย์ซึ่งควรคัดลอกข้อมูล) โดยจะคัดลอกไปจนสิ้นสุดอาร์เรย์
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<script type="text/javascript">
var int32View = new Int32Array([21, 64, 89, 65, 33, 66, 87, 55 ]);
document.write("Contents of the typed array: "+int32View);
int32View.copyWithin(5, 0);
document.write("<br>");
document.write("Contents of the typed array after copy: "+int32View);
</script>
</body>
</html> ผลลัพธ์
Contents of the typed array: 21,64,89,65,33,66,87,55 Contents of the typed array after copy: 21,64,89,65,33,21,64,89