Computer >> Máy Tính >  >> Lập trình >> Javascript

Hàm TypedArray.copyWithin () trong JavaScript

Hàm copyWithin () của đối tượng TypedArray sao chép nội dung của TypedArray này bên trong chính nó. Phương thức này chấp nhận ba số trong đó số đầu tiên đại diện cho chỉ số của mảng mà tại đó việc sao chép các phần tử sẽ được bắt đầu và hai số tiếp theo đại diện cho các phần tử bắt đầu và kết thúc của mảng mà từ đó dữ liệu sẽ được sao chép (lấy).

Cú pháp

Cú pháp của nó như sau

obj.copyWithin(3, 1, 3);

Ví dụ

<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>

Đầu ra

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

Ví dụ

Không bắt buộc phải truyền tham số thứ ba cho hàm này (các phần tử cuối của mảng mà từ đó dữ liệu sẽ được sao chép), nó sẽ sao chép cho đến cuối mảng.

<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>

Đầu ra

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