This returns a jQuery object. what is a jQuery object. Is it an object, an array, or some combination of both?
$("#id")
I’m looking in the source here, but can not find it.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
First, what it’s not.
A jQuery object is not an Array.
In JavaScript, there are built-in native constructor functions. One of these is
Array. But ultimately theArrayconstructor creates Objects. jQuery objects are not built from the Array constructor.So how is an Object different from an Array?
Since
ObjectandArrayare built-in native constructors, the objects created from the constructors have an internal[[Class]]property. You can see its value like this.So you can see that these objects have that as a difference.
What other differences are there?
The prototype chain of the two objects are different. For a plain object, it could be visualized like this.
While for an Array, like this.
So you can see that their inheritance distinguishes them as well.
So what about a jQuery object?
A jQuery object is more like a plain Object than an Array. But JavaScript lets you define custom (not built-in) constructors.
The
toStringvalue will be the same as an Object[object Object], but the prototype chain will be different.So jQuery’s prototype chain would be similar to this, but with the
jQueryconstructor instead ofFoo.So what does all this mean?
All objects in JavaScript are similar in that they inherit from
Object.prototype*, but you can have different objects that have an extended prototype chain, and you can also have native objects that have an internal[[Class]]property that gives them distinction.So to answer the question of “what type of object is a jQuery object”, the answer is that it is an Object that inherits from
Object.prototypelike every object, but also inherits from theprototypeof its custom constructor.* Note that in ES5, you can also have an object that has no prototype chain. Its chain is terminated immediately with
null.But a jQuery object stores DOM elements at numeric indices, and has a
.lengthproperty. Doesn’t that make it an Array?No, that just makes it an object with properties that are numbers, and a property named
length.An Array’s properties are not special. They are identical to an Object’s properties.
These two code examples are doing the exact same thing.
They’re doing exactly the same thing? Really?
Well almost. The properties themselves are no different between Array objects and Object objects, but Arrays have some special behaviors.
For example, if I add a property at a higher index than the current
.lengthaccounts for, the.lengthwill be automatically adjusted.On an Array,
.lengthitself has some “magic” abilities, like being able to truncate the Array by setting.lengthto a lower value.So while a jQuery object has numeric properties and a
.lengthproperty, it doesn’t behave as a native Array would behave.