You have to use functions in that code, because you are using two callbacks, 
and that's how you do callbacks in JavaScript, with
functions that get called later.

The first callback is the outer function that gets called when the document is 
ready.

The second callback is the inner function that gets called when you click an A 
tag.

Is it the nested anonymous functions that are throwing you off? You can 
certainly substitute named functions if you prefer. This
code does the same thing:

   $(document).ready( ready );
   
   function ready() {
      alert( 'Ready' );
      $('a').click( clicked );
   }
   
   function clicked() {
      alert( 'Clicked' );
   }

But I agree with Erik: Even if you choose to use named functions in your own 
code, it would be a good idea to get comfortable with
anonymous functions. You will see a LOT of them in jQuery code, and you don't 
want to feel dizzy every time.

It's no different from setTimeout. You can code:

   setTimeout( function() {
      alert( 'Hello' );
   }, 1000 );

or the equivalent:

   setTimeout( hello, 1000 );

   function hello() {
      alert( 'Hello' );
   }

One other point: Do you have an editor that does brace/bracket/parenthesis 
matching and syntax checking (not just syntax coloring)?
If not, I highly recommend the free Komodo Edit:

http://www.activestate.com/Products/komodo_edit/

Even if you have another favorite editor, it's worth loading your code into 
Komodo in addition, just to get the syntax checking.

-Mike

> From: A32
> 
> I find the following example very dirty syntax:
> 
> $(document).ready(function(){
>       alert("Document is ready")
> 
>       $("a").click(function(){
>               alert("Clicked");
>       });
> });
> 
> With all those ) and } I don't know if I'm coming or going.. Is there
> an alternate syntax I can use? Do I *have* to use the "function()" all
> the time or is there a different way? I've been away from JavaScript
> for a long time but never seen anything like that :-)
> 
> If there's no way around it, does anybody know of a javascript
> preprocessor that I could use a cleaner syntax while developing?

Reply via email to