Why use an array-like object in Javascript?

I get this because javascript allows numeric keys to be used on objects, so array-like objects are technically possible, but why have they become commonplace?

Perhaps the thought was that these array-like objects don't just have numeric keys, for example. arguments

has a property callee

, so they may not be suitable arrays to host those properties.

But in javascript it is great for treating an array as an object and using non-numeric keys:

var myArguments = []; 
myArguments[0] = 0; 
myArguments['callee'] = function(){console.log('callee')};

      

If an object is like an array and can access functions that it might otherwise inherit from the array prototype, would it be helpful to make it an array-like object?


Edit: In case it wasn't clear in my question that an array-like object would be like an object arguments

that has sequential numeric properties starting at zero and has a length property that is one less than the highest numeric key. It also does not inherit from the array prototype and does not provide access to array methods.

+3


source to share


1 answer


Actually an array in JS is an object :)



As long as you want an array, it's a bad idea to create array type objects because JS engines optimize the speed of arrays. However, it is possible.

-2


source







All Articles