Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basic question about function() 1

Status
Not open for further replies.

minli98

IS-IT--Management
Aug 30, 2005
178
US
Hi,

I see that some functions are written as:

Code:
variable = function(){

}
What does this mean? And how is it different from the conventional function?

Thank you.

Min
 
This means that the function is called and that function returns a value that is assigned to variable.

Like this:

var myvalue = myfunction();

function myfunction()
{
return true;
}

Very basic example of course. What happens is that the myfuntion function returns true so when you set myvalue you are setting it to the result of the myfunction function.



Paranoid? ME?? WHO WANTS TO KNOW????
 
Look at the OP's example more carefully. What it is doing is creating an "anonymous" function, and assigning the function itself to the variable, not the return value of the function. Here's a better example:
Code:
var something = function() {
  alert("anonymoun function");
  return "some return value";
}
If you execute the statement:
[tt] alert(something)'[/tt]
what will be displayed in the alert box is:
[tt] function() {
alert("anonymoun function");
return "some return value";
}[/tt]
however, if you execute the statement:
[tt] something();[/tt]
you will get the alert that says "anonymous function".

The variable becomes a reference to the anonymous function.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Sorry I need to clarify my question:

The conventional function that I know of has the format of
Code:
returnVar = function functionName(){

}

But I have seen declaration of functions without name, such as
Code:
returnVar = function(){

}

Does this mean that the function will automatically be run?

And then I am also baffled by lines like the following. This is from (function dropdownmenu).

Code:
dropmenuobj.onmouseout=ie5? function(){ dynamichide(event)} : function(event){ dynamichide(event)}

Since dynamichide(event) is already a declared function, why the need to put function() in front? And what's the difference between function() and function(event)?

Thank you once again.

Min
 
Thanks tsdragon for the explanation. It's much clearer now. Please ignore my previous follow-up. I posted it before I read your posting.

Thank you all!

Min
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top