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