JavaScript typedArray.length()用法示例介绍

2021年5月4日23:09:31 发表评论 85 次浏览

typedArray.length是JavaScript中的内置属性, 用于返回给定typedArray的长度。

语法如下:

typedArray.length

参数:

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

返回值:

它返回给定typedArray的长度。

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

代码1:

<script>
  
   //Creating a typedArray with some elements
   var A = new Uint8Array([5, 10, 15, 15, 5, 20, 10]);
   var B = new Uint8Array([5, 10]);
   var C = new Uint8Array([5, 10, 15]);
   var D = new Uint8Array([5, 10, 15, 15, 5]);
   var E = new Uint8Array([5, 10, 15, 15, 5, 20]);
    
   //Calling length() function
   b = A.length     
   c = B.length    
   d = C.length  
   e = D.length
   f = E.length
    
    
   //Printing the length of the typedArray
   document.write(b + "<br>" );
   document.write(c + "<br>" );
   document.write(d + "<br>" );
   document.write(e + "<br>" );
   document.write(f + "<br>" );
    
</script>

输出如下:

7
2
3
5
6

代码2:

<script>
  
   //Creating some ArrayBuffer with a size in bytes
   const A = new ArrayBuffer(8);
   const B = new ArrayBuffer(6);
   const C = new ArrayBuffer(16);
   const D = new ArrayBuffer(32);
    
   //Creating some typedArray with the above sizes 
   //of the buffer
   const E = new Uint8Array(A);
   const F = new Uint8Array(B);
   const G = new Uint8Array(C);
   const H = new Uint8Array(D);
    
   //Calling length property
   a = E.length
   b = F.length
   c = G.length
   d = H.length
  
   //Printing the length of the typedArray
   document.write(a + "<br>" );
   document.write(b + "<br>" );
   document.write(c + "<br>" );
   document.write(d);
    
</script>

输出如下:

8
6
16
32

一盏木

发表评论

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