Javascript array best practice to use [] instead of new array?

前端 未结 5 1114
醉话见心
醉话见心 2020-12-31 04:08

I read at many tutorials that the current best practices to create a new javascript array is to use

var arr = [] 

instead of



        
5条回答
  •  情话喂你
    2020-12-31 04:21

    It's less typing, which in my book always wins :-)

    Once I fixed a weird bug on one of our pages. The page wanted to create a list of numeric database keys as a Javascript array. The keys were always large integers (a high bit was always set as an indicator). The original code looked like:

     var ids = new Array(${the.list});
    

    Well, guess what happened when the list had only one value in it?

     var ids = new Array(200010123);
    

    which means, "create an array and initialize it so that there are 200 million empty entries".

提交回复
热议问题