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

后端 未结 1 544
一向
一向 2020-12-09 04:35

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

相关标签:
1条回答
  • 2020-12-09 05:20

    This is what Firebug's isArray method does: (from the 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 that the object is a true JavaScript array, but they do a reasonable job of guessing whether an object is a pseudo-array, which in turn gives you a convenient array-like representation for debugging.

    Chrome may or may not use these same checks, and the new Web Console in Firefox 4 doesn't recognize anything other than true arrays as arrays.

    0 讨论(0)
提交回复
热议问题