4 ms·
Wow! Thanks for taking the time to reply. :) Sorry, I did go a bit far. I'd forgotten that 'delete' is the only way to remove items from an array in Javascript
by mhansen 17y ago
Wow! Thanks for taking the time to reply. :) Sorry, I did go a bit far.
I'd forgotten that 'delete' is the only way to remove items from an array in Javascript, which you need to do all the time. x_x
I just found another example: in a chat server, removing sessions that have timed out from the session list. http://github.com/ry/node_chat/blob/master/server.js#L94 http://github.com/ry/node_chat/blob/master/server.js#L94
Thanks for writing this post! You helped me understand more than just 'delete' - your explanation of the Global, Function and Eval contexts helped me understand closures better.
- kangax 17y agoActually, deleting items from array is usually done with `Array#splice`. Using `delete` operator only deletes a property, but doesn't update Array object's `length`, and it is this `length` that matters when iterating over an array (with `for(;;)`); when appending to array (using `Array#push`), and in few other cases. In other words: var array = [1,2,3,4]; delete array[2]; array.length === 4; // length is still 4 array.join() === '1,2,,4'; // which can lead to these kind of unexpected results