What makes Firebug/Chrome console treat a custom object as an array?

When I am developing in jQuery, I frequently find myself typing selectors into the Chrome/Firebug console and seeing what they give me. They are always nicely formatted as if they were arrays:

Chrome's console shows a jQuery selection as an array

I am trying to work out what it is that makes the console treat an object as an array. For instance, the following custom object is not treated as an array:

function ElementWrapper(id) {
    this[0] = document.getElementById(id);
}

Chrome's console shows the object as a normal object

If I then add a length property and a splice method, it magically works as an array, with any properties with integer keys treated as members of the arrays:

function ElementWrapper(id) {
    this[0] = document.getElementById(id);
    this.length = 1;
    this.splice = Array.prototype.splice;
}

Chrome's console shows the object as if it was an array

So essentially my question is: what determines whether the console displays an object as an array? Is there any rationale to it, or is it a completely arbitrary "if an object has these properties, it must be an array?" If so, what are the decisive properties?

14
задан lonesomeday 9 February 2011 в 22:02
поделиться