Skip to content Skip to sidebar Skip to footer

How Is $('h1') Logging To The Web Console As An Array In Jquery?

If you do console.log($('some selector')) in the browser, it returns what looks like an array (first line): But notice that it's not an instanceof Array, but it's actually the jQu

Solution 1:

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

functionSomeType() {
    this.push(16);
}

SomeType.prototype = [];
SomeType.prototype.constructor = SomeType; // Make sure there are no unexpected resultsconsole.log(newSomeType()); // 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!

Post a Comment for "How Is $('h1') Logging To The Web Console As An Array In Jquery?"