I got another question regarding jQuery's architecture. $('div')
constructs a new jQuery
object:
$('div') instanceof jQuery; // true
I'd like to know why it is possible to query it like an array, allthough it isn't an array?
$('div')[0]; // returns the first div in the document as a DOM node.
$.isArray($('div')); // false
I just love this syntax, it looks so clean! I also noticed this returns the DOM nodes as an array:
console.log($('div'));
Can somebody explain me how to implement this behaviour to my own objects?
My own approach was to make an array with some 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 doesn't seem to be the way jQuery does it as this is actually an array:
$.isArray(a); // true
I'd like to know how jQuery does this to learn and to see if it's a better solution than mine.