Why does jQuery.fn display [] in the console?
javascript, jquery
Solution
`jQuery.fn` simply meets the requirements of being array-like for the developer consoles. It's not actually an `Array` instance*, but it has an interface that affords being treated as an array.
*If `jQuery.fn` were actually an array, `jQuery.fn instanceof Array` would evaluate to `true`; it doesn't. It does copy some of the `Array.prototype` methods though.
If you want to check if an object is actually an `Array`, there are two means, the simplest is `obj instanceof Array`, however this will be true for objects that inherit from `Array`. If you want to check that an object is an Array, but need to exclude objects that inherit from `Array` you should use:
function isArray(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
}
Example:
var a, b;
function Foo() {}
Foo.prototype = [];
a = new Foo();
b = [];
a instanceof Array; //true
b instanceof Array; //true
isArray(a); //false
isArray(b); //true
Problem
I am trying to understand how jQuery is coded. They have an object: ``` jQuery.fn = { //key value pairs } ``` But if I type `jQuery.fn` in the browser console, it just returns `[]` and not the object itself. Does anyone know why?