简体   繁体   中英

Why use an array-like object in Javascript?

I get that since javascript allows numeric keys for objects, the existence of array-like objects is therefore technically possible, but why did they ever become common?

Maybe the thought was that these array-like objects don't just have numeric keys, eg arguments has the callee property, so they can't be proper arrays to accommodate those properties.

But in javascript, it's perfectly valid to treat an array as an object and use non-numeric keys:

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

If the object is array-like and would benefit from having access to the functions it would otherwise inherit from the array prototype, what would be advantage of making it an array-like object instead?


Edit: in case it wasn't clear in my question, an array like object would be something like the arguments object that has sequential numeric properties starting from zero, and has a length property that is one less than the highest numeric key. It also doesn't inherit from the array prototype, and doesn't provide access to array methods.

There is no big advantage, as you can access objects and arrays in very similar ways, array[number] for arrays, and object.propery , and object["property"] for objects. Arrays are better for storing data, not functions, in my opinion, and objects are better for storing sets of functions, in my opinion. For example, arrays could store your answers in a memory game, but an object would store the command for the game, like start or end.

I think the biggest advantage of using Array-Like object, is an object itself. An object has less overhead than an Array. Can read: Array vs. Object efficiency in JavaScript

The other advantage of using objects, Language does not have to create/allocate contiguous memory as in Array. The object is more dynamic in nature. The array has to create a memory block in advance. and update on overload.

Read more: How are the JavaScript Arrays internally resizing?

Another factor, I think lookup in a map or object is faster.

By array-like objects, I think you mean something like this:

const nativeObject = { 0: '@@Object', 1: 842713 };

One advantage would be the dynamic behaviour of the objects. For example, let's assume that you want to add new behaviour for your object.

 const nativeObject = { 0: '@@Object', 1: 842713, toString() { console.log(`[Name: ${this[0]}, Id: ${this[1]}]`); }, }; nativeObject.toString()

Overall objects offer high extendability and expressibility in the type of their keys compared to arrays.

Actually Array in JS is an Object :)

As long as You need array, it is bad idea to create array-like objects, because JS engines optimize speed of arrays. Yet, its possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM