Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a jQuery Object?

JavaScript kind of redefines what an Array means because an array is an object with a .length property and methods like .slice() and .join().

jQuery defines a jQuery object as "Array like", because it has a length property but it doesn't have certain array methods like join().

If I were to define the jQuery object as an object, and forget about mentioning anything to do with an array, how would I define it? What properties does it have besides length?

I guess all the methods are what you see in the documentation, far exceeding the number of methods that are in an array.

like image 739
Phillip Senn Avatar asked Jun 22 '11 19:06

Phillip Senn


People also ask

What is object object in jQuery?

[object Object] is a string representation of an object. You may see this text if you use alert() to print an object to the screen, for instance. You can view the contents of an object using console.

What is the difference between the jQuery function and the jQuery object?

A jQuery object is an object returned by the jQuery function. A jQuery object represents a set of document elements and can also be called a “jQuery result”, a “jQuery set”, or a “wrapped set”.

What does the $() mean in jQuery?

In jQuery, the $ sign is just an alias to jQuery() , then an alias for a function. This page reports: Basic syntax is: $(selector).action() A dollar sign to define jQuery. A (selector) to "query (or find)" HTML elements.

How do you define an object in JavaScript?

Creating a JavaScript ObjectCreate a single object, using an object literal. Create a single object, with the keyword new . Define an object constructor, and then create objects of the constructed type. Create an object using Object.create() .


2 Answers

A jQuery object is array-like which means that it contains zero or more indexes (properties which names are positive integers starting with zero). Besides those indexes, a jQuery object contains these properties:

  • length
  • context
  • selector

And also around 140 inherited methods (which are defined on the jQuery.prototype object - you can do console.dir(jQuery.prototype) to get a full list... ).

Note that jQuery objects do not contain (or inherit) the Array methods (slice, substr, ...). If you want to execute those methods on your jQuery object, use call/apply.


For example, if you have 3 TEXTAREA elements on the page and you do this:

var j = $('textarea');

then this j jQuery object will contain these properties:

  • 0 - reference to the first TEXTAREA element
  • 1 - reference to the second TEXTAREA element
  • 2 - reference to the third TEXTAREA element
  • length - which is 3
  • context - reference to the document object
  • selector - which is 'textarea'
  • plus all those inherited methods...
like image 72
Šime Vidas Avatar answered Sep 30 '22 20:09

Šime Vidas


the jQuery object is an object which has

  • a length property
  • numeric properties which reference the items from the select (0,1,2,3...)
  • bindings to jQuery functions
  • additional jQuery properties

The length and numeric properties allow the object to respond like an array. You can run it in a for loop or use functions like map or each on it.

I would recommend using your browser's debugger or Firebug and inspect a jQuery object. That will teach you a lot about how it is structured.

like image 32
The Who Avatar answered Sep 30 '22 21:09

The Who