JavaScript typedArray.buffer()和typedArray.byteLength()用法示例

2021年5月6日17:06:37 发表评论 109 次浏览

typedArray.buffer()是JavaScript中的一个属性, 它表示由typedArray引用的ArrayBuffer和该属性

typedArray.byteLength()表示typedArray的长度(以字节为单位)。

语法如下:

typedArray.buffer
typedarray.byteLength

参数:

它不接受任何参数, 因为它是属性而不是函数。

返回值:

它不返回任何值。

JavaScript代码显示此属性的工作方式:

代码1:

<script>
  
     //creating some ArrayBuffers with a size in bytes
     const buffer1 = new ArrayBuffer(8);
     const buffer2 = new ArrayBuffer(12);
     const buffer3 = new ArrayBuffer(20);
     const buffer4 = new ArrayBuffer(22);
     const buffer5 = new ArrayBuffer(4);
  
     //Creating typedArray object for above buffers
     const A = new Uint16Array(buffer1);
     const B = new Uint16Array(buffer2);
     const C = new Uint16Array(buffer3);
     const D = new Uint16Array(buffer4);
     const E = new Uint16Array(buffer5);
  
     //Getting the length of the arrayBuffer
     document.write(A.byteLength + "<br>" );
     document.write(B.byteLength + "<br>" );
     document.write(C.byteLength + "<br>" );
     document.write(D.byteLength + "<br>" );
     document.write(E.byteLength);
      
</script>

输出如下:

8
12
20
22
4

一盏木

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: