4

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.

  • This is the element represented by the selector. – samuelesque Sep 24 '12 at 18:25
  • In Firefox, use `console.debug($("#id"));`. – Travesty3 Sep 24 '12 at 18:26
  • see this SO link. Might be helpful. http://stackoverflow.com/questions/1302428/what-does-jquery-actually-return – samuelesque Sep 24 '12 at 18:27
  • 2
    @HiroProtagonist: An array-like object is a regular object with properties that make it look like an array, as I explained in my answer. – SLaks Sep 24 '12 at 18:29
  • It is an Array of DOM elements. Another link. http://www.vfstech.com/?p=40 Edit: Not actually an array. "The jQuery object itself behaves much like an array" Taken from http://api.jquery.com/Types/#jQuery – samuelesque Sep 24 '12 at 18:31
  • 1
    @dotsamuelswan: It isn't an Array. – I Hate Lazy Sep 24 '12 at 18:31
  • [jQuery's Docs](http://api.jquery.com/Types/#jQuery) explain what it is. Basically, it's an object that behaves similar to an array, but is not a javascript array. – MrOBrian Sep 24 '12 at 18:31
  • I will happily upvote if you find more of an answer. I don't know that what you are looking for exists. – samuelesque Sep 24 '12 at 18:43
  • I think the confusion might come from the simplicity of the question. You are just asking "what is it?" so we are answering "it's an object" (and providing a bunch of links to a lot more information). What, exactly, are you wanting to know about the jQuery object? Are you expecting it to return something that is built in to javascript? Please do dig into the source and let us know what you find that answers your question. – MrOBrian Sep 24 '12 at 18:43
  • @HiroProtagonist Are you expecting to find some line of code that says, `return new jQuery.fn.init( selector, context, rootjQuery );`? Because I can save ya the trouble: https://github.com/jquery/jquery/blob/master/src/core.js#L30 – Pete Sep 24 '12 at 18:43
  • @SLaks It's not helping that some people are offering conflicting and _inaccurate_ information. – canon Sep 24 '12 at 18:46
  • @HiroProtagonist: care to point to the source? Some jQuery methods are meant to convert a jQuery object to an actual Array, but that doesn't mean that a jQuery object *is* an Array. Methods like `.toArray`, `$.makeArray` and `$.map` do this. – I Hate Lazy Sep 24 '12 at 21:59
  • ...oh, I didn't see your updated question. Just because `$.makeArray` returns an Array, doesn't mean that a jQuery object is an Array. If a jQury method returns `null`, would that mean that a jQuery object is the same as `null`? – I Hate Lazy Sep 24 '12 at 22:03
  • Also, note that here: `return jQuery.makeArray( selector, this )`, it is using an internal method signature. With that call, `ret` refers to the `jQuery` object, and `arr` is being pushed into the jQuery object. Then the jQuery object is returned. The main `$` function will never return an Array. – I Hate Lazy Sep 24 '12 at 22:10

4 Answers4

16

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 the Array constructor creates Objects. jQuery objects are not built from the Array constructor.


So how is an Object different from an Array?

Since Object and Array are built-in native constructors, the objects created from the constructors have an internal [[Class]] property. You can see its value like this.

Object.prototype.toString.call({}); // [object Object]
Object.prototype.toString.call([]); // [object Array]

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.

{} ---> Object.prototype ---> null

While for an Array, like this.

[] ---> Array.prototype ---> Object.prototype ---> null

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 toString value will be the same as an Object [object Object], but the prototype chain will be different.

function Foo() {

}

new Foo() ---> Foo.prototype ---> Object.prototype ---> null

So jQuery's prototype chain would be similar to this, but with the jQuery constructor instead of Foo.


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.prototype like every object, but also inherits from the prototype of 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 .length property. Doesn't that make it an Array?

No, that just makes it an object with properties that are numbers, and a property named length.

var myObj = {};
myObj[0] = "foo";
myObj[1] = "bar";

An Array's properties are not special. They are identical to an Object's properties.

var myArr = [];
myArr[0] = "foo";
myArr[1] = "bar";

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 .length accounts for, the .length will be automatically adjusted.

myArr.length; // 2
myArr[9] = "baz";
myArr.length; // 10

On an Array, .length itself has some "magic" abilities, like being able to truncate the Array by setting .length to a lower value.

myArr.length = 1;
myArr[1]; // undefined

So while a jQuery object has numeric properties and a .length property, it doesn't behave as a native Array would behave.

I Hate Lazy
  • 47,415
  • 13
  • 86
  • 77
  • @HiroProtagonist: Global describes their accessibility in the variable scope. By "built-in", I mean that they are part of the environment, and conform to ECMAScript. I think "native" may be the term the spec uses. I'll double check and update if needed. – I Hate Lazy Sep 24 '12 at 18:46
  • The dom elements are stored as numbered properties of the main object. – canon Sep 24 '12 at 18:48
  • @HiroProtagonist: I'll give an update, but DOM elements are just stored as numeric properties. Array properties are identical to Object properties. There's literally no difference. `myObj[0] = "foo"` does the exact same thing as `myArr[0] = "foo"`. – I Hate Lazy Sep 24 '12 at 18:50
  • @HiroProtagonist: I just updated. Yes, they're just properties (keys) on the object. All objects in JavaScript represent properties the same way. If I remember, internal jQuery also has a method for pushing collections of DOM elements into a jQuery object one at a time, but it is basically doing the same thing in a loop. `this[i] = collection[i]; i++;` – I Hate Lazy Sep 24 '12 at 19:03
  • @HiroProtagonist: Sorry, that's pseudo code. jQuery has different techniques for adding new items to an Array. One is seen [here in its `jQuery.merge`](https://github.com/jquery/jquery/blob/1.8.2/src/core.js#L669-672). Another can be found if you search for `core_push`, which is really just a reference to `Array.prototype.push`. They invoke it using the jQuery object as the calling context, and it operates on the jQuery object as though it was an Array. – I Hate Lazy Sep 24 '12 at 19:18
  • @HiroProtagonist: Why did you delete all your comments? Now mine don't make sense. – I Hate Lazy Sep 24 '12 at 22:16
12

$() return an instance of the jQuery class (new jQuery()).
It therefore inherits all standard jQuery methods, as well as all plugins, from jQuery.prototype (which is aliased to jQuery.fn)

In addition, the jQuery() constructor turns each instance into an array-like object, by adding a lengthproperty, as well as indexed properties for each element in the set.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
4

The returned object (see EcmaScript §4.2.1 on what that means) is an instance of jQuery's internal constructor function (jQuery.fn.init - see this answer for detailed information on jQuery's instantiation pattern), inheriting from the $.fn prototype object.

And of course, it is an instance of Object as nearly every object in JavaScript.

And, because every jQuery element represents a collection of [DOM] elements, it looks like an Array with its numeric index properties - although the length property isn't self-updating automatically, you can apply most of the Array methods on it. It is not a real Array instance (What's an Array, and what is in JS?).

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 1
    a jQuery object! :-) -- JavaScript is loosely typed. So the "kind" of object is immaterial. Any object that provides the utility you require at the moment is the "right type". – Pete Sep 24 '12 at 18:31
  • 2
    @HiroProtagonist It "looks like an Array", but it's not an Array. Inheriting from Array is dangerous, so it inherits from Object and implements array-like features (e.g. `.length`) -- In other words, it quacks like an Array. ;-) – Pete Sep 24 '12 at 18:32
  • @HiroProtagonist: It only looks like, Pete is exactly right. The jQuery prototype even has some Array methods on it, like `sort` which you can invoke directly – Bergi Sep 24 '12 at 18:43
  • Yes, there is currently no way to inherit from the Array "class" in JS. You might google around for the approaches… – Bergi Sep 24 '12 at 19:21
2

jquery object is a object of jquery library basically all the jquery methods and properties are attached to that jquery object that you can;t use it as a normal javascript object. it also vary from which jquery object it is.

Apurv
  • 309
  • 2
  • 10
  • there is no term called class in javascript all we have is functional constructor we called them class. – Apurv Sep 24 '12 at 18:31
  • What i understood is. we have a functiional constructor like function MyObject(){this.name="ABC"} and your create your own custom object like var myFirstObject=new MyObject(). PLease correct me if i am wrong – Apurv Sep 24 '12 at 19:16