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:
i trying work out makes console treat object array. instance, following custom object not treated array:
function elementwrapper(id) { this[0] = document.getelementbyid(id); }
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; }
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
Post a Comment