Copy link to clipboard
Copied
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?
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));
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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()
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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));
}
Copy link to clipboard
Copied
That looks a lot more compact, thanks
Copy link to clipboard
Copied
you're welcome.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now