Jorik
Jorik

Reputation: 239

What object does jquery return exactly?

I've been working on my own personal JavaScript library for a while now, and it works fine. But I've been wondering about the jQuery return object.

Lets say you have a few divs in your DOM and you select them with $("div") jquery actually returns the selected nodes (as an object/array?) in the console log and you can mouse-over them to see where they are in the documents.

My object actually returns the entire object itself, so if you call kj("div") (Where kj is my object name) it shows up like this in the console log:

    > kj
    > elements: Array[10]
    > length : 10
    > more stuff

My question is, how can I make it return something like jQuery?

Thanks in advance.

Upvotes: 7

Views: 1160

Answers (3)

Gabriel
Gabriel

Reputation: 18780

I think what you are looking for is that in jQuery the Array of elements is the primary object, the methods and other information is connected to that array as properties.

function $$(tagname){
  var x = document.getElementsByTagName( tagname );
  x.moreStuff = true;
  return x;
}

var d = $$('div');

because typeof Array === 'object' you can arbitrarily attach methods and properties to an array.

Upvotes: 3

Gats
Gats

Reputation: 3462

JQuery hooks up it's own references to an object whick in turn reference to things in the dom. Those references are a little more complex than just the "contents of the html" as there are events attached. JQuery also has very efficient "Selectors" that iterate over the dom and build those references.

I have to say I agree with the Scrum Meister. JQuery's an accepted standard across even Microsoft development these days (WOOHOO!). Why not use it?

Upvotes: 3

S L
S L

Reputation: 14318

Open you console on this page and do console.log($('#custom-header')) and you will get the result. I think that, jquery will return an object with following methods and property which we use on them like hide() and show(). I think it is better to use jquery then to create another library.

>>> console.log($('#custom-header'))
[div#custom-header]



0
    div#custom-header


context
    Document what-is-the-jquery-returned-object-exactly


jquery
    "1.4.4"


length
    1


selector
    "#custom-header"


init
    function()


TextAreaResizer
    function()


_toggle
    function()


add
    function()


addClass
    function()


addSpinner
    function()


addSpinnerAfter
    function()


after
    function()


ajaxComplete
    function()


ajaxError
    function()


ajaxSend
    function()


ajaxStart
    function()


ajaxStop
    function()

    .......
    .......
    .......

Upvotes: 0

Related Questions