Delegation in AS3

Blog / ActionScript ·

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

Krasimir Tsonev With over two decades of deep programming expertise, I offer comprehensive web consultancy and stack audits, alongside specialized workshops, training, and engaging public speaking to elevate your team's skills and optimize your digital presence. Contact me.

Keywords: function param2 func param1 delegation
Share the post on Twitter, Facebook, LinkedIn