Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Although jQuery objects act like arrays, they are actually only <em>array-like</em> objects. An array-like object is an object using numeric keys and having a <code>length</code> property - that is the minimum needed for compatibility with the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Methods" rel="noreferrer">native array methods</a>.</p> <p>Because jQuery objects are only array-like and not actual <code>Array</code> objects, native array operations (like <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf" rel="noreferrer"><code>indexOf</code></a> or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse" rel="noreferrer"><code>reverse</code></a>) cannot be called directly. You can use <code>Array.prototype</code> though, or extend jQuery's functionality.</p> <pre><code>$('div').reverse(); // TypeError: $("div").reverse is not a function // we can use Array.prototype though Array.prototype.reverse.apply($('div')); // or we can extend jQuery very easily $.fn.reverse = Array.prototype.reverse; $('div').reverse(); // now it works! </code></pre> <p>You are correct in your assumption that Firebug does not include any special-casing for formatting jQuery objects. A quick search reveals <a href="http://www.mail-archive.com/firebug@googlegroups.com/msg01548.html" rel="noreferrer">a relevant post</a> on the Firebug mailing list. Assuming the information is still correct (the post is from January) Firebug will format an object as an array if it has a <strong>finite length</strong> and a <strong><code>splice</code> method</strong>.</p> <p>JQuery fulfils both of these criteria, but <a href="https://github.com/jquery/jquery/blob/fef476def30f4b1f3cfa902fff11bb761ab6ecfd/src/core.js#L273" rel="noreferrer">their implementation of <code>splice</code></a> is nothing more than a direct copy of the native <code>Array</code> method. It is undocumented, which means it's either only for internal use, or perhaps added solely for the purpose of tricking Firebug into formatting jQuery objects nicely.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload