Ezilon.com - Target Your Audience, be Seen in Your Region

myfunction = function() { //function }

You are viewing this site as a guest. Join our community to get your questions answered and share knowledge. Active members may advertise and ask for a website critique.

They have: 299 posts

Joined: Feb 2005

Im not sure about the type of function declaration that is shown in the example below:

onload=function() {
for(c=0;c<anc.length;c++) {
if(anc[c].className=='pup') {
   anc[c].onclick=function() {
   popUp(this.href);
   return false;
    }
   }
  }
}

'

Or is this how you declare a recursive function (a function that calls itself)?

JeevesBond's picture
Moderator

He has: 3,523 posts

Joined: Jun 2002

It looks like a function for recursing through all the links on a page, changing their onclick handlers to run the popUp function. Does that make sense? There's got to be more to it than that, anc must be defined somewhere: getElementByTagName or something like that.

a Padded Cell our articles site!

They have: 299 posts

Joined: Feb 2005

Thanks for the reply jeeves, i understand what it is supposed to do what i dont understand is why you declare the function like:

something = function(){

}

rather than:

function(){

}

JeevesBond's picture
Moderator

He has: 3,523 posts

Joined: Jun 2002

Ah, I see what you're getting at. It's attaching the function to an onload event, if it were just:

function dave() {
  ... code ...
}

'
That by itself wouldn't do anything, just define a function. It would work just as well to do:

function dave() {
  ... code ...
}
window.onload = dave;

'
But that's not as neat. What you first posted is a good example of Javascript's support for functions as first-class objects. Simply put: functions are treated just like variables in Javascript, it's pretty good stuff. Smiling

a Padded Cell our articles site!