I have another question about jQuery architecture. $('div') creates a new jQuery object:
$('div') instanceof jQuery; // true
I would like to know why it is possible to request it as an array, although this is not an array?
$('div')[0]; // returns the first div in the document as a DOM node. $.isArray($('div')); // false
I just like this syntax, it looks so clean! I also noticed that this returns the DOM nodes as an array:
console.log($('div'));
Can someone explain to me how to implement this behavior for my own objects?
My own approach was to create an array with methods like this:
var a = ['a', 'b', 'c']; a.method = function(){ return 'test'; }; a; // ['a', 'b', 'c'] a[0]; // 'a' a.method(); // 'test'
However, this does not look like jQuery does, as it is actually an array:
$.isArray(a); // true
I would like to know how jQuery does this to find out, and find out if this is a better solution than mine.
javascript jquery
js-coder
source share