PDA

View Full Version : [SOLVED] Function Definition Questions



msinn
23 Dec 2010, 1:53 AM
Hello everybody,

I know, this is a beginner question, but I'm just a beginner and I hope someone here would be nice to explain the differences between those forms of function definitions:

1.




var myFunc1 = function(args){
//do something
};

//later in the script then:

myFunc1(args);

2.




function myFunc2(args){
//do something
};

//later in the script then:

myFunc2(args);

3.




(function myFunc3(args){
//do something
});

4.




var myFunc4 = function(args){
//do something
}();

So please tell me, when tu use the different function definition forms and if there are important differences to pay attention to avoid troubles, I hope my Question isn't so stupid for you.

THX, and have a nice day!

Condor
23 Dec 2010, 1:58 AM
#1 and #2 are identical; both define a function (myFunc1/2) and execute it.
#3 defines the myFunc3 function, but doesn't execute it.
#4 executes an anonymous function and assigns the result to myFunc4.

msinn
23 Dec 2010, 2:37 AM
Thank You so much Condor,

so you mean that:

#1 and #2 while the script is being parsed and executed by the browser interpreter those functions are executed and return a value (void or some data types) but those return values get lost in Nirvana. There are NO differences between var myFunc1 = function(){} or function myFunc2(){}

#3 all my functions that I'll be using later in my code should be defined according to this definition form, because I'll be using them later and why should they be first executed when the codeline is reached by the interpreter

#4 so in this case var myFunc4 isn't (mustn't be) a function. It is just the returned result (could be also a function)

If I understand you, then please get a big THX, otherwise slap a little bit more my beginner ignorance.