[ Web Proxy ]
URL:
Viewing: https://fa.javascript.info/arraybuffer-binary-arrays [Back]  [Original]

ArrayBuffer,
:
DanskEnglishEspaolFranaisIndonesiaItalianoTrkeOzbek

ArrayBuffer,

( ) . .

.

. :

  • ArrayBuffer, Uint8Array, DataView, Blob, File, etc.

. .

ArrayBuffer .

:

let buffer = new ArrayBuffer(16); //      16
alert(buffer.byteLength); // 16

16 .

ArrayBuffer

. ArrayBuffer :

  • .
  • .
  • View buffer[index].

. . ArrayBuffer

View ArrayBuffer

ArrayBuffer eyeglasses . view

:

  • Uint8Array 0 255 .( 8 .). 8- ArrayBuffer
  • Uint16Array 0 65535 . 16- integer 2
  • Uint32Array 0 4294967295 . 32- integer 4
  • Float64Array 1.8x10308 5.0x10-324 float 8

( 8 ) float 16 16 8 ( 2 ) 4 ( 4 ) 2 ArrayBuffer

[]

ArrayBuffer

view

let buffer = new ArrayBuffer(16); //      16

let view = new Uint32Array(buffer); //          32 

alert(Uint32Array.BYTES_PER_ELEMENT); //      4 

alert(view.length); // 4,         
alert(view.byteLength); // 16,  

//   
view[0] = 123456;

//   
for(let num of view) {
  alert(num); //  123456  0  0  0(  4 )
}

TypedArray

view (Uint8Array Unit32Array ) TypedArray . .

TypedArray view ArrayBuffer : Int8Array Uint8Array .

new TypedArray new Int8Array new Uint8Array .

Typed : .

Typed( Int8Array Float64Array ) .

5 :

new TypedArray(buffer, [byteOffset], [length]);
new TypedArray(object);
new TypedArray(typedArray);
new TypedArray(length);
new TypedArray();
  1. ArrayBuffer view . .

    byteOffset ( 0 ) length ( ) view .

  2. typed .

    :

    let arr = new Uint8Array([0, 1, 2, 3]);
    alert( arr.length ); // 4,       
    alert( arr[1] ); // 1,  4 (   8-)      
  3. TypedArray : typed . .

    let arr16 = new Uint16Array([1, 1000]);
    let arr8 = new Uint8Array(arr16);
    alert( arr8[0] ); // 1
    alert( arr8[1] ); // 232,    1000      1000   8   (  )
  4. length typed . length TypedArray.BYTES_PER_ELEMENT :

    let arr = new Uint16Array(4); //  4    typed  
    alert( Uint16Array.BYTES_PER_ELEMENT ); //      2 
    alert( arr.byteLength ); // 8 (  )
  5. typed .

TypedArray ArrayBuffer . view ArrayBuffer ( ) @.

ArrayBuffer TypedArray :

  • buffer ArrayBuffer
  • byteLength ArrayBuffer

view :

let arr8 = new Uint8Array([0, 1, 2, 3]);

//     view 
let arr16 = new Uint16Array(arr8.buffer);

typed :

  • Uint8Array, Uint16Array, Uint32Array 8 16 32
    • Uint8ClampedArray ( ) clamps 8
  • Int8Array, Int16Array, Int32Array ( )
  • Float32Array, Float64Array 32 64
int8 - - .

Int8Array int int8 .

Int8Array view ArrayBuffer .

typed

8 0 255 Uint8Array . 256 100000000(9 ) Uint8Array 256

: 8 ( )

[]

.

: 257 100000001(9 ) 8 1

[]

28

:

let uint8array = new Uint8Array(16);

let num = 256;
alert(num.toString(2)); // 100000000 ( )

uint8array[0] = 256;
uint8array[1] = 257;

alert(uint8array[0]); // 0
alert(uint8array[1]); // 1

Uint8ClampedArray . 255 255 0 . .

TypedArray

TypedArray .

map slice find reduce .

:

  • splice typed view . .
  • concat

:

  • arr.set(fromArr, [offset]) fromArr arr offset .( 0 .)
  • arr.subarray([begin, end]) view begin end (). slice ( .) view .

typed .

DataView

DataView view untyped ArrayBuffer . DataView offset .

  • typed . . i arr[i] .

  • DataView .getUint8(i) .getUint16(i) . .

:

new DataView(buffer, [byteOffset], [byteLength])
  • buffer . typed DataView . ArrayBuffer
  • byteOffset ( 0 )view
  • byteLength ( )view

:

let buffer = new Uint8Array([255, 255, 255, 255]).buffer; //   4       255 

let dataView = new DataView(buffer);

//  offset    8  
alert( dataView.getUint8(0) ); // 255

//             65535    offset     16  
alert( dataView.getUint16(0) ); // 65535 (    16 )

//   offset    32  
alert( dataView.getUint32(0) ); // 4294967295 (    32 )

dataView.setUint32(0, 0); //      4        

DataView . ( 16 32 ) DataView .

ArrayBuffer .

ArrayBuffer view .

  • TypedArray :
    • Uint8Array, Uint16Array, Uint32Array 8 16 32
    • Uint8ClampedArray clamps 8
    • Int8Array, Int16Array, Int32Array ( )
    • Float32Array, Float64Array 32 64
  • DataView view getUint8(offset)

typed ArrayBuffer . buffer. view .

:

  • ArrayBufferView view .
  • BufferSource ArrayBuffer ArrayBufferView .

. BufferSource ArrayBuffer view .

( ) :

[]

Uint8Array . concat(arrays) .

sandbox .

function concat(arrays) {
  // sum of individual array lengths
  let totalLength = arrays.reduce((acc, value) => acc + value.length, 0);

  let result = new Uint8Array(totalLength);

  if (!arrays.length) return result;

  // for each array - copy it over result
  // next array is copied right after the previous one
  let length = 0;
  for(let array of arrays) {
    result.set(array, length);
    length += array.length;
  }

  return result;
}

sandbox.


Web Proxy Viewer  |  New URL  |  Original Page