Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

ฟังก์ชัน TypedArray.copyWithin() ใน JavaScript

เมธอด copyWithin() ของ TypedArray

เมธอด copyWithin() ของอ็อบเจกต์ TypedArray ใช้คัดลอกลำดับของอิลิเมนต์ภายในอาร์เรย์เดียวกัน (in-place) โดยไม่เปลี่ยนขนาดของอาร์เรย์ คล้ายกับ Array.prototype.copyWithin() แต่ทำงานกับ TypedArray (เช่น Int32Array, Float64Array, Uint8Array, ฯลฯ)

รูปแบบไวยากรณ์ (Syntax)

typedArray.copyWithin(target, start, end)

พารามิเตอร์

  • target (บังคับ): ดัชนีเริ่มต้นที่จะวางข้อมูลที่คัดลอกมา (เริ่มที่ 0) หากเป็นค่าลบ จะนับจากท้ายอาร์เรย์
  • start (ตัวเลือก): ดัชนีเริ่มต้นของข้อมูลต้นทางที่จะคัดลอก (ค่าเริ่มต้น 0) หากเป็นค่าลบ จะนับจากท้ายอาร์เรย์
  • end (ตัวเลือก): ดัชนีสิ้นสุดของข้อมูลต้นทาง (ไม่รวมดัชนีนี้) ค่าเริ่มต้นคือความยาวของอาร์เรย์ หากเป็นค่าลบ จะนับจากท้ายอาร์เรย์

ตัวอย่างที่ 1: คัดลอกพร้อมระบุ end

<html>
<head>
  <title>JavaScript TypedArray copyWithin 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 + "<br>");
    
    // คัดลอกจากดัชนี 0-4 (5 อิลิเมนต์) ไปวางที่ดัชนี 5
    int32View.copyWithin(5, 0, 5);
    
    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

ตัวอย่างที่ 2: ไม่ระบุ end (คัดลอกจนถึงท้ายอาร์เรย์)

<html>
<head>
  <title>JavaScript TypedArray copyWithin Example 2</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 + "<br>");
    
    // คัดลอกจากดัชนี 0 จนถึงท้าย ไปวางที่ดัชนี 5
    int32View.copyWithin(5, 0);
    
    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

ตัวอย่างเพิ่มเติม: ใช้ค่าลบ (Negative Indices)

var arr = new Int32Array([1, 2, 3, 4, 5, 6, 7, 8]);

// คัดลอก 3 ตัวท้ายไปวางที่ 3 ตัวแรก
arr.copyWithin(0, -3); 
// ผลลัพธ์: [6, 7, 8, 4, 5, 6, 7, 8]

// คัดลอกจากดัชนี 1 ถึง ตัวที่ 3 จากท้าย ไปวางที่ดัชนี 5
arr.copyWithin(5, 1, -2);
// ผลลัพธ์: [1, 2, 3, 4, 5, 2, 3, 4]

คุณสมบัติสำคัญ

  • In-place: เปลี่ยนแปลง TypedArray เดิมโดยตรง ไม่สร้างอาร์เรย์ใหม่
  • ไม่เปลี่ยนขนาด: ความยาวของ TypedArray คงที่ตลอด
  • ทับซ้อนได้ (Overlapping safe): อัลกอริทึมจัดการการทับซ้อนของพื้นที่ต้นทางและปลายทางได้อย่างถูกต้อง (copy จะเกิดขึ้นเหมือนคัดลอกข้อมูลไปที่ buffer ชั่วคราวก่อน)
  • ทำงานกับ TypedArray ทุกชนิด: Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array, BigInt64Array, BigUint64Array

กรณีการใช้งาน (Use Cases)

  • การเลื่อนข้อมูลใน buffer (shift data)
  • การทำ Circular Buffer / Ring Buffer
  • การประมวลผลสัญญาณดิจิทัล (DSP) และการทำงานกับ Audio/Video data
  • WebGL / WebGPU vertex buffer manipulation
  • การเพิ่มประสิทธิภาพหน่วยความจำ (avoid allocation)

เบราวเซอร์รองรับ

รองรับในเบราวเซอร์สมัยใหม่ทุกตัว (Chrome 45+, Firefox 34+, Safari 10+, Edge 14+)