Is it an antipattern to set an array length in JavaScript?

前端 未结 7 830
广开言路
广开言路 2020-12-08 13:18

Is it bad to use code like:

var a = [1,2,3,4];
a.length = 2; // 3 and 4 are removed

Does it have decent browser support? Do the removed val

7条回答
  •  感情败类
    2020-12-08 13:46

    It is not an anti pattern. It is supported in all major browsers. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length

提交回复
热议问题