Is it possible to create a fixed length array in javascript?

后端 未结 9 1124
春和景丽
春和景丽 2020-12-03 00:59

Is it possible, in Javascript, to create an array whose length is guaranteed to remain the same?

For example, the array A is created with length 2. Subs

9条回答
  •  难免孤独
    2020-12-03 02:00

    Actually to create a fully optimized true c like fixed array in js on most modern browsers (including IE 11) you could use: TypedArray or ArrayBuffer like so:

    var int16 = new Int16Array(1); // or Float32Array(2)
    int16[0] = 42;
    console.log(int16[0]); // 42
    int16[1] = 44;
    console.log(int16[1]); // undefined
    

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray

提交回复
热议问题