JavaScript中的TypedArray.copyWithin()函数
TypedArray 对象的 copyWithin() 方法在自身内复制此 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
广告