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

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

TypedArray.reverse()

เมธอด reverse() กลับลำดับสมาชิกใน Typed Array (In-place) สมาชิกตัวแรกจะกลายเป็นตัวสุดท้าย และในทางกลับกัน

ไวยากรณ์

typedArray.reverse()

ตัวอย่าง

<html> <body> <script>
  var typedArray = new Int32Array([11, 5, 13, 4, 15, 3, 17, 2, 19, 8]);
  document.write("Contents of the typed array: " + typedArray);
  document.write("<br>");
  var result = typedArray.reverse(); // ไม่ต้องส่งพารามิเตอร์
  document.write("Contents of the reversed array: " + result);
</script> </body> </html>

ผลลัพธ์

Contents of the typed array: 11,5,13,4,15,3,17,2,19,8
Contents of the reversed array: 8,19,2,17,3,15,4,13,5,11

หมายเหตุ: ตัวอย่างโค้ดต้นฉบับมี typedArray.reverse(Math.sqrt) ซึ่งผิดพลาด เพราะ reverse() ไม่รับพารามิเตอร์ใดๆ