What exactly is an anonymous function?

Is there a difference between:

[AS]advance = function(){…[/AS]

and

[AS]function advance(){…[/AS]

I believe one of them is known as an anonymous function, but I am not sure which one or why.

Along those same lines… If I am on frame 1 of a movie clip, is there a difference between:

[AS]this.advance = function(){…[/AS]

and

[AS]advance = function(){…[/AS]

Thanks.

In your first example, the first one is an anonymous function, however as you
are using it, it makes no difference. A different example will make the reason why it’s called an anonymous function more plain.

Consider the following use of a named function.



function randomizePosition()
{
    this._x = random(300);
    this._y = random(200);
}

mymovie.onEnterFrame = randomizePosition;
myothermovie.onEnterFrame = randomizePosition;


“randomizePosition” is a named function. I can assign it to any number of
movie clips. If I had used the anonymous style syntax, it would have looked like this.



mymovie.onEnterFrame = function ()
{
    this._x = random(300);
    this._y = random(200);
}

// this is kind of uglY:
myothermovie.onEnterFrame = mymovie.onEnterFrame;


As you can see, it’s anonymous because the function wasn’t assigned a name (“randomizePosition”). This syntax is a little uglier when you’re going to be assigning the function to multiple movies. However, for one-shot movies, anonymous functions are fine, and produce slightly smaller code.

In terms of performance, it doesn’t really matter which method you choose.

My personal preference is to use named functions because most of my programming life I’ve been programming in languages which only support that kind (C for example).

  • Jim