JavaScript typedArray.byteOffset属性用法

2021年5月6日17:05:39 发表评论 186 次浏览

typedArray.byteOffset是JavaScript中的一个内置属性, 用于返回给定typedArray从其ArrayBuffer开始处的偏移量(以字节为单位)。

语法如下:

typedArray.byteOffset

参数:

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

返回值:

它从其ArrayBuffer的开头返回给定typedArray的偏移量(以字节为单位)。

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

<script>
  
     //Constructing some ArrayBuffers
     var buffer1 = new ArrayBuffer(2);
     var buffer2 = new ArrayBuffer(8);
     var buffer3 = new ArrayBuffer(16);
     var buffer4 = new ArrayBuffer(32);
  
     //Constructing some typedArray with
     //parameter of above buffers
     var A = new Uint8Array(buffer1);
     var B = new Uint8Array(buffer2, 4);
     var C = new Uint8Array(buffer3, 5);
     var D = new Uint8Array(buffer4, 8);
  
    //Calling byteOffset property
     a = A.byteOffset;
     b = B.byteOffset;
     c = C.byteOffset;
     d = D.byteOffset;
  
    //Printing the offset in bytes of
    //the above typedArray from the start
    //of its ArrayBuffer
    document.write(a + "<br>" );
    document.write(b + "<br>" );
    document.write(c + "<br>" );
    document.write(d);
  
</script>

输出如下:

0
4
5
8

一盏木

发表评论

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