Reputation: 911
I came across them, but I have yet to see why I should use them. Could someone explain it?
Upvotes: 9
Views: 9066
Reputation: 400
It is comfortably to use in closures, when you need not name for this function. I. e. this way:
var myFunc : Function = function() : void {trace("Hello world!");}
doSomething(myFunc);
or this:
button.addEventListener(MouseEvent.CLICK,
function(e : MouseEvent) : void
{
// doSomething
});
You do not have to use them, but you may if it is easier.
Upvotes: 11
Reputation: 2363
At the risk of oversimplifying, you can think of anonymous functions as "throwaway" functions that you need to create at runtime. Here's an example of where might you use one:
Imagine you have some buttons on screen for changing colors and the handler for each of them looks like this (pseudocode, please overlook syntax errors):
private function redBtnHandler(): void {
GlobalBrush.setColor(0xff0000);
}
private function greenBtnHandler(): void {
GlobalBrush.setColor(0x00ff00);
}
private function blueBtnHandler(): void {
GlobalBrush.setColor(0x0000ff);
}
...
redBtn.setClickHandler(redBtnHandler);
greenBtn.setClickHandler(greenBtnHandler);
blueBtn.setClickHandler(blueBtnHandler);
...
It's painful to create a bunch of these, not to mention hard-to-edit later on.
Instead, you'd use a "function factory" to generate handlers, like so:
private function generateBtnHandler(rgb:uint): Function {
var btnHandler:Function = function(): void {
GlobalBrush.setColor(rgb);
}
return btnHandler;
}
redBtn.setClickHandler(generateBtnHandler(0xff0000));
greenBtn.setClickHandler(generateBtnHandler(0x00ff00));
blueBtn.setClickHandler(generateBtnHandler(0x0000ff));
In "set-and-forget" situations like the above, these are extremely handy.
Hope the above example helped make it clear why anyone would use these things.
Upvotes: 3
Reputation: 1863
http://www.adobe.com/devnet/actionscript/learning/as3-fundamentals/functions.html
http://www.alanmacdougall.com/blog/post/3/
Or
An anonymous function is defined like this:
bar = function(param1, param2, etc) {
// do stuff here
}
Anonymous functions are interesting beasts. Unlike named functions, when you create an anonymous function there is no way to reference the function via code. We can get around this with function literals.
When the function is created it returns a reference to itself. In the example above, we set a variable "bar" to reference the anonymous function we create.
Now, whenever bar is called, the anonymous function that it references will execute. Unlike named functions, anonymous functions cannot be forward referenced. If I make a call to bar before I set bar to reference the function, the call will fail.
When creating an anonymous recursive function, the function can reference itself by using the arguments.callee property. Consider this example:
// classic factorial recursion example
factorial = function(n) {
if (n <= 1) {
return n;
} else {
return n*arguments.callee(n-1); // call ourself to recurse
}
}
Note that in the above example, we can replace arguments.callee with factorial and the example will still work. However, there are times in which we don't have a variable to call the function with. In those cases, arguments.callee is the only solution. An example of this might be passing an anonymous function as a parameter to another function.
Function literals can define functions in any timeline/object, provided the path exists at the time of definition. For instance, to create a function inside of an object, we have to use a function literal like this:
obj = new Object();
obj.foo = function() {
return "bar";
}
trace(obj.foo()); // bar
Upvotes: 1
Reputation: 22604
Essentially, an anonymous function allows you to create very fine-grained variations to behavior, without having to create a subclass, or to encapsulate what would otherwise have to be a complex switch statement into a short, clean method block: Where you would have to make a lot of decisions based on state, you can now simply assign a function to perform a certain task at runtime. Think of it like any variable - only this special kind of variable doesn't have a value, but a behavior.
The standard example for this is event listeners, but you can also apply this to any other functionality you desire.
You can find out about anonymous functions here, and subsequently learn about the concept of closures at Wikipedia. Admittedly, the Wikipedia source is a bit heavy with technical terms, but Martin Fowler also has a quite readable blog post on this topic.
Upvotes: 4