Simple tip #5 Create function to call any function with unknown args

The other day I wanted to create a function in a class that would take a Function as a parameter and an Array of arguments.  Much like callLater() does, but not doing the whole queuing thing until the next frame.

So how do you call a function that may have any number of arguments. Well here is the code and it should speak for itself.

 protected function checkSomethingThenCallOtherFunction(
    method : Function, args : Array = null ) : void
 {
    if( something.length < someMaxLimit )
    {
        method.apply( null, args );
    }
    else
    {
        //do something else
    }
}

So how easy is that? Pass in the function and an array of arguments, that’s all.

Previous Tip

Next Tip

[ad name=”ad-1″]

One thought on “Simple tip #5 Create function to call any function with unknown args

Leave a Reply

Your e-mail address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.