function ab() vs ab=function() 


In summary, I recommand use "ab=function(){}" instead of "function ab(){}". Just do this and skip this article. If you are really interested in the difference then read it.


EXAMPLE 1: function declaration is at the begining of a section.

//Syntax 1: does not work;
on(press){
    ab();
    ab=function(){
        trace("ab=fun");
    }
}

//Syntax 2: works
on(press){
    ab();
    function ab(){
        trace("fun ab()");
    }
}


Syntax 1 does not work until the next button click. When we say "ab=function(){};", we create a function object and return that handle to the variable "ab". We access the function by this variable. In this example, we call ab(), but we have not yet define what is the variable "ab", so this call does not work. The next click will work because "ab" is now defined.

Syntax 2 works. When Flash compiler read the syntax "function ab(){}", it treats that block of code as a declaration of a function. It create a function with the name "ab" and it does not return any reference out. Traditionally , function declaration should be at the begining of the code session. When the script is compiled to swf, every function declaration block will be isolated out and moved to the begining of this section. In fact in the swf, the script is changed to:

on(press){
    function ab(){
        trace("calling ab");
    }
    ab();
}

That is why the syntax 2 works.

This might not suprise C programmers. It is acceptable to declare function at the end of the c file. However, it might appears weird for action scripter that we can call a function before it is declared. OK, in fact, the call is not "before" the declaration when we understand the end result in the compiled swf. 


EXAMPLE 2: missing function declaration block in SWF

a = true;
if (a) {
    trace("a is true");
    ab = function () {
   
             trace("ab=fun");
    };
};
ab();

//Syntax 2: will not work;
a = true;
if (a) {
    trace("a is true");
    function ab() {
        trace("fun ab()");
    };
};
ab();


Syntax 1 works. Since a is true, a function object is created and return its handle to the variable ab. We can call it no problem.

Syntax 2 has special problem. We get an output - "a is true" and then nothing. The calling of ab() does not trace out any output.

When Flash try to compile the script, Flash notice a function declaration. Flash will try to isolate that function declaration block and put them at the begining of this section. However, there is an "if". If the function block is moved to the beginning of this section, then it is isolated out of the "if block". Then, no matter a is true or not, the function gets declared and ab() will always work. This is not what I mean to do by this script.

Yes, it is the result in Flash 5. If we copy the script to Flash 5; It will trace out by ab(). We may try to set a=false, and the calling of ab() still works. Even we call ab() at the first line before "a=true"; It still works. So, the function gets declared at the beginning of the section.

Does Flash MX behave more reasonable ? I dont know this is more reasonable or not. In the case of Flash MX, it will not put the function declaration to the beginning of this section. Flash MX just think it is invalid and should be comment out. So in the swf, we can not find that function declaration. Dont try to access that function. It does not exist in the swf. In the swf, the script is compiled as:

a=true;
if(a){
    trace("a is true");
}
ab();

It is very hard to debug why the function call fails in such condition.


EXAMPLE 3: the scope of function declaration:

//Syntax 1: works;
function defFun() {
    ab = function () {
        trace("ab=fun");
    };
}
defFun();
ab();


//Syntax 2: will not work;
function defFun() {
    function ab() {
        trace("calling ab");
    }
}
defFun();
ab();


Syntax 1 works. When defFun() is called, it define a variable "ab" in that timeline, maybe _root.ab; This variable accept a reference of a function object. Well, we have no problem to access a variable in the timeline. Of-course it works.

Syntax 2 does not work. When defFun() is called, we declare a function with name of "ab"; System allocate memory pieces for varible "ab and function object". The scope of "ab" is not in the timeline. It is like declaring a variable by "var ab". The script is similar to "var ab=function(){};" There are no way to access those variable outside of that function. When we say ab(), we are accessing a variable in the timeline (maybe _root), and of course it is not there.