View Full Version : function myFunc(arg) vs. myFunc = function(arg){
walterppk
05-11-2004, 09:42 AM
I am studying objects and functions and I can not figure out what is the difference and when you would use each.
function myFunc(arg){
// do some stuff
}
myFunc = function(arg){
// do some stuff
}
:confused:
CyanBlue
05-11-2004, 09:50 AM
Howdy... :)
The only difference is the creation time of the function...
For example, try this code...trace(test1("hello"));
trace(test2("hello"));
function test1(arg)
{
return arg;
}
test2 = function (arg)
{
return arg;
}You will get hello and undefined... You get undefined for the test2() function call because the function test2 is not defined yet when the second trace line is executed...
The same code, different order...function test1(arg)
{
return arg;
}
test2 = function (arg)
{
return arg;
}
trace(test1("hello"));
trace(test2("hello"));Now, you get two hello lines because you are calling test2 function after it is defined...
So, to cap... function myFunc(arg) { ... } always gets compiled and available to the calls in the beginning of the movie, and myFunc = function (arg) { ... } gets compiled when that line is executed...
walterppk
05-11-2004, 12:40 PM
excellent CyanBlue you are most generous with your details. Thank you kindly! :)
CyanBlue
05-11-2004, 01:40 PM
I am not such a good guy... :p
Glad to be of your service... :)
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.