Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does jQuery.length == 2?

For both current stable jQuery versions (1.11.0 & 2.1.0), I've noticed that jQuery.length == 2 after jQuery is loaded.

Why is that ?

Both jQuery[0] and jQuery[1] are undefined.

I'm familliar with the jQuery array-like objects and console behavior regarding arrays/objects and objects with .length and .splice().

Just wondering about jQuery.length

like image 580
TiTi Avatar asked Feb 11 '14 19:02

TiTi


2 Answers

jQuery is a function, and the length property of a function equals the number of formal (declared) parameters to the function. From MDN:

length is a property of a function object, and indicates how many arguments the function expects, i.e. the number of formal parameters. This number does not include the rest parameter. By contrast, arguments.length is local to a function and provides the number of arguments actually passed to the function.

Specifically, the jQuery function has this signature (source):

jQuery = function( selector, context ) {
    ...
}
like image 191
p.s.w.g Avatar answered Oct 20 '22 21:10

p.s.w.g


jQuery is a function.
The length property of a function returns the number of declared parameters.

like image 35
SLaks Avatar answered Oct 20 '22 22:10

SLaks