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


when developing in jquery, find myself typing selectors chrome/firebug console , seeing give me. nicely formatted if arrays:

chrome's console shows jquery selection array

i trying work out makes console treat object array. instance, following custom object not treated array:

function elementwrapper(id) {     this[0] = document.getelementbyid(id); } 

chrome's console shows object normal object

if add length property , splice method, magically works array, properties integer keys treated members of arrays:

function elementwrapper(id) {     this[0] = document.getelementbyid(id);     this.length = 1;     this.splice = array.prototype.splice; } 

chrome's console shows object if array

so question is: what determines whether console displays object array? there rationale it, or arbitrary "if object has these properties, must array?" if so, decisive properties?

this firebug's isarray method does: (from firebug source)

if (!obj)     return false; else if (isie && !isfunction(obj) && typeof obj == "object" && isfinite(obj.length) && obj.nodetype != 8)     return true; else if (isfinite(obj.length) && isfunction(obj.splice))     return true; else if (isfinite(obj.length) && isfunction(obj.callee)) // arguments     return true; else if (instanceof(obj, "htmlcollection"))     return true; else if (instanceof(obj, "nodelist"))     return true; else     return false; 

of course, none of these checks ensures object true javascript array, reasonable job of guessing whether object pseudo-array, in turn gives convenient array-like representation debugging.

chrome may or may not use these same checks, , new web console in firefox 4 doesn't recognize other true arrays arrays.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -