My 5th book "50 Tips on JavaScript" is on sale. Go get it from https://50tips.dev.
Illustrated guide to 50 of the most interesting JavaScript topics.

Delegation in AS3

I just can't code without delegation. I'm using it every day and wanted to share this method with you.
Here is the class:
 package {
    public class Delegate {
      public static
      function create(handler: Function, ...args): Function {
        return function (...innerArgs): void {
          handler.apply(this, innerArgs.concat(args));
        }
      }
    }
  }
The usage:
import flash.utils.setTimeout;
    setTimeout(Delegate.create(func, "param1text", "param2text"), 1000);
    
    function func(param1: String, param2: String): void {
      trace("func param1=" + param1 + " param2=" + param2);
    }
And the result of the code:
func param1=param1text param2=param2text
If you enjoy this post, share it on Twitter, Facebook or LinkedIn. To leave a comment go here.