myfunction = function() { //function }

They have: 426 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

He has: 3,956 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: 426 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

He has: 3,956 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!

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.