Is there a reason JavaScript developers don't use Array.push()?

前端 未结 6 1179
庸人自扰
庸人自扰 2020-12-01 05:29

I commonly see developers use an expression like the following in JavaScript:

arr = []
arr[arr.length] = \"Something\"
arr[arr.length] = \"Another thing\"
         


        
6条回答
  •  情歌与酒
    2020-12-01 05:36

    It's a way to limit nested braclets. If you have enough of them you cant see howmany there are or howmany you need (when later looking at the code). I would use a var, one should only have to count things one time.

    bar = foo.length;
    foo[ bar++ ] = "new item 0";
    foo[ bar++ ] = "new item 1";
    foo[ bar++ ] = "new item 2";
    foo[ bar++ ] = "new item 3";
    

    http://jsfiddle.net/vEUU3/

提交回复
热议问题