Why does jQuery.fn display [] in the console?

I am trying to understand how jQuery is encoded.

They have an object:

jQuery.fn = {
     //key value pairs
}

But if I type jQuery.fnin the browser console, it will simply return [], not the object itself. Does anyone know why?

+3
source share
2 answers

jQuery.fnjust matches the array requirements for developer consoles . This is not an actual instance Array*, but it has an interface that allows you to process an array.

* If it jQuery.fnwas actually an array, jQuery.fn instanceof Arraywould appreciate true; this is not true. However, it copies some of the methods Array.prototype.


, Array, , - obj instanceof Array, , Array. , , , Array, :

function isArray(arg) {
    return Object.prototype.toString.call(arg) === '[object Array]';
}

:

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
+7

jQuery , jQuery.fn -

+2

All Articles