> From: R. Rajesh Jeba Anbiah
> 
>      I'm little confused with the architecture of jquery. I 
> tried to understand it with Firebug, but I'm lost as it's 
> just showing this tree.
> 
>     As far as I understand, $() creates a new jQuery object. 
> So, if we use $() many times, will it create as many objects 
> for it? TIA

Yes.

Other than not requiring you to say "new", it is just like any other
JavaScript constructor. If you call "new Date()" many times, it will create
that many Date objects.

Imagine you had a function like this:

   function NewDate() {
      return new Date();
   }

That would obviously create a Date object every time you call it:

   var date = NewDate();

Now try this one:

   function MyObject() {
      if( this === window )
         return new MyObject();
   }

This one gets a little more interesting. You can call MyObject as a
constructor:

   var myObject = new MyObject();

Or you can leave out the "new" and it will do the exact same thing:

   var myObject = MyObject();

The "this === window" test catches the case of calling the function without
the "new", and it does the "new" for you.

If you look at the code for $()/jQuery(), you'll see that it calls "new
jQuery" internally in this way, at line 19 of jquery-1.1.4.js.

-Mike

Reply via email to