What is an Array-Like?

8

A given string is an array-like, and a given number is not. Already their respective objects are array-likes (the objects themselves), right? An object declared, a DOM Object is also an arraylike, correct? Everything is array-like minus boolean number data and operators. All this is very confusing for me.

    
asked by anonymous 10.11.2014 / 23:25

1 answer

10

In JavaScript (and potentially other languages but I do not remember any) there is a Array data type that has a length property (indicating its size), the ability to have its individual elements accessed by numeric indexes ( x[0] ), and several other methods belonging to the type.

However, except for a few small details (more on this later), there is no significant difference between an array and a "normal" object. If you create an object of type:

var objeto = { "0":"foo", "1":"bar", "2":"baz", length:3 };

You can use it in many situations as if it were a native array:

for ( var i = 0 ; i < objeto.length ; i++ )
    console.log(objeto[i]);

What you can not do is to use native methods of Array - since this object does not have this type as a prototype (by default - if you want, you can give this prototype to it ):

objeto.forEach(function() { ... }); // undefined não é um método

Another difference is that in an array, adding new elements updates its length property automatically, whereas in an ordinary object this does not happen:

var arr = [];
$("#saida").append("<p>" + arr.length + "</p>");
arr[0] = 10;
$("#saida").append("<p>" + arr.length + "</p>");

var obj = { length: 0 };
$("#saida").append("<p>" + obj.length + "</p>");
obj[0] = 10;
$("#saida").append("<p>" + obj.length + "</p>");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="saida"></div>

Because there is no significant difference, it is very common for a JavaScript library to allow programmers to use their objects as if they were arrays, even when it is inconvenient to use a "real" array. In this case, the object is said to be "array-like" or in English "array-like" . The best example I have of an array-like is the "jQuery object":

var ps = $("p"); // Selecionei todos os elementos p
ps.html("foo"); // O retorno é um objeto jQuery, com suas próprias funções

for ( var i = 0 ; i < ps.length ; i++ ) // Mas também posso usá-lo como se fosse um array
    ps[i].innerHTML = "foo";

Knowing whether an object is array or array-like is only important when it is used in a way specific to arrays. Often, to allow a larger range of input values (i.e. without limiting to objects of type Array ) one chooses to program in a way that works for both arrays and array-likes . The% w / w of% above was an example. Another would be to use the for functions referenced explicitly in your prototype:

Array.prototype.forEach.call(objeto, function() { ... }); // Funciona em ambos os casos

Or, to add an element and ensure that Array is updated:

var objeto = { length:0 };
Array.prototype.push.call(objeto, 10); // Depois dessa chamada, length é 1

P.S. Also it is good to emphasize that in JavaScript all the properties are textual. Even in an object of type length , when you do:

var arr = [1,2,3];
arr[1] = 4;

Internally this is represented as if it were:

var arr = Object.create(Array.prototype, { "0":1, "1":2, "2":3, "length":3 });
arr["1"] = 4;

Other languages may have different implementations, and different considerations as to what an array-like is or not (or not allow array-likes at all). Formally speaking, for an object to be considered X-like it must have an implicit interface that is compatible to some extent with type "X". That is, even though it does not belong to the class, and this class does not have an explicit contract, that object still sufficiently fulfills its implicit contract so that it can be used in practice as an instance of that type.

Needless to say, this concept only makes sense in languages with dynamic typing or duck-typing - if typing is static, the only thing that can be used instead of an array is a subtype of array (which, by inheritance relation "is an array, does not look like an array).

    
10.11.2014 / 23:52