Skip to main content
Inspiring
July 28, 2014
Answered

Storing functions with arguments as String

  • July 28, 2014
  • 2 replies
  • 687 views

I need to store a function with arguments in one string.

Without arguments, you can simply do sth. like that:

functionName:String = "hello";

this[functionName]()

function hello():void{

   "Hello, World";

}

But as soon as you introduce arguments it`s getting tricky.

I came up with this:

function getArgsFromFunction(_function: String): Array {

    var argstart: int = _function.indexOf("(");

    var argend: int = _function.indexOf(")");

    var argstring: String = _function.substr(argstart + 1, argend - argstart - 1);

    var args: Array = argstring.split(",");

    return args;

}

function getNameFromFunction(_function: String): String {

    var nameEnd: int = _function.indexOf("(");

    var nameString: String = _function.substr(0, nameEnd);

    return nameString;

}

var f:String = "showMessage(A,B)";

function showMessage(_args: Array): void {

    trace(_args);

}

var _funct: Function = this[getNameFromFunction(f)];

_funct.call(this, getArgsFromFunction(f));


but its not very versatile, not very robust, and seems unnecessarily complicated.

Anyone know a quicker way to get this done?

This topic has been closed for replies.
Correct answer kglad

function parseF(s:String):void{

    var a:Array = s.split("(");

    var arg:Array = a[1].split(",");

    this[a[0]](arg[0],arg[1].substr(0,-1));

}

2 replies

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
July 28, 2014

function parseF(s:String):void{

    var a:Array = s.split("(");

    var arg:Array = a[1].split(",");

    this[a[0]](arg[0],arg[1].substr(0,-1));

}

Inspiring
July 29, 2014

That looks a lot more compact, thanks

kglad
Community Expert
Community Expert
July 29, 2014

you're welcome.

Amy Blankenship
Legend
July 28, 2014

Take a step back and explain what it is you're trying to do. You are correct that this is a terrible way to do it (whatever "it" is), but the ickness starts as "I want to store everything as a string" and permeates from there.

Inspiring
July 29, 2014

I have a static class that mainly functions as a mediator between my main and other classes. This static class has a dictionary that looks like this:

    public static var dict:Dictionary = [
   {index: 1, effect: "upgrade(front,1)",condition:"!isFront(empty,self)" }....}]

I want to be able to call the stored function like this:

Function(dict[0].effect).call()

Amy Blankenship
Legend
July 29, 2014

Here are some cleaner alternatives:

Instead of using a static class as a mediator, create an EventDispatcher and pass a reference to it to the instances that need to communicate (in other words, an event bus). Have them listen on the event bus and call their own methods when an event that concerns them occurs.

Go ahead and register your instances (but not with a static Class--that's just asking for trouble), but have them implement an Interface. That way, you already know what methods are available and what arguments they take.

As a variation on the above, create a "communication" method that takes a generic object that's defined by the Interface supplied by your instances. Pass in the object and let each Class worry about what parts of the Object it cares about.