I always thought that jQuery $
function returns an array with jQuery methods attached to it.
I want to provide some examples:
Let's say we have an array
var arr = [1,2,3];
Then we are able to add our own properties:
arr.someProp = 10;
arr.someMethod = function() { ... }
After that arr
remains an array despite having custom properties:
arr instanceof Array; //true
So, I thought jQuery object is something like arr
(but more complicated) until recent experiment.
I just run this code:
$('div') instanceof Array; //false (!!!)
But it behaves like an array. It has push
method, length
property, that works correctly even in such case:
var $jq = $('div');
$jq.length; //3
$jq.push(123); //wrong code, I know, this is just for test
$jq.length //4
Also if you execute console.log($('div'))
, it will output something like this:
[<div></div>, <div></div>, <div></div>]
Besides jQuery object has some methods that are equal Array.prototype
methods:
$('div').sort === Array.prototype.sort; //true
$('div').splice === Array.prototype.splice; //true
My questions is: how this thing is being created?
So if you guys explain me this and provide some code samples, I would be very grateful to you.