How is $('h1') logging to the web console as an array in jQuery? How is $('h1') logging to the web console as an array in jQuery? jquery jquery

How is $('h1') logging to the web console as an array in jQuery?


You make your object inherit Array using the prototype, like so:

function SomeType() {    this.push(16);}SomeType.prototype = [];SomeType.prototype.constructor = SomeType; // Make sure there are no unexpected resultsconsole.log(new SomeType()); // Displays in console as [16]

And, of course, all jQuery objects are instances of the jQuery function/constructor, so that's how jQuery does it. As a bonus, because of the inheritance, you get all the methods from Array, and the indexing that comes with it too!