2

for example i have a string

    var string:String = new String("myFunc(para1, para2);");

is there any way i could use this value of string as an AS3 statement?

note: the amount of parameters may vary, as the value of string will be decided dynamically, so if you are suggesting me to use string properties to seperate parameters and function name and then use it like in many tutorials :-

    this[string](para1, para2);

that wont solve my problem.

2
  • 1
    Have you tried function.apply? Commented Aug 2, 2014 at 5:45
  • just read about it, that should work. Commented Aug 2, 2014 at 8:02

2 Answers 2

1

I believe you could still solve your problem by breaking your string up. You can write your method/function to take in a variable amount of parameters using the 'rest' parameter or the arguments object. Both of which are mentioned here with examples:http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f56.html

Based off my comment, some sample code:

// this just takes in a bunch of numbers and returns the sum
function myFunc(...args:Array):Number {
    var result:Number = 0;
    for(var counter:int = 0; counter < args.length; counter++){
        result += args[counter];
    }
    return result;
}

// can manually call it and pass in as many parameters as you want
myFunc(1,2,3); // will return 6
myFunc(1,2,3,4,5); // will return 15
myFunc(1,2,3,4,5,6,7,8,9,10); // will return 55


// and based on your comment:
var functionName:String = "myFunc";
var parameters:Array = new Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
// since you've mentioned your parameters in an array already, can just use the apply() call as Pan suggested
this[functionName].apply(this, parameters); // will return 210
Sign up to request clarification or add additional context in comments.

3 Comments

lets say i have separated the parameters and function name. a string named str("myFunc") and an array named arr(para1, para2, para3). help me with code to call this function.
actually, if you have the parameters separated in its own array, you could just pass in that array to your method/function and work off of that. But I'll add an example using the rest parameter/arguments object anyways.
should have read the other answer before I edited, but it looks like Jason's answer has the same thing as well as a suggestion on solving it another way
0

If you're referring to dynamically evaluating and executing expressions by interpreting code from strings, ActionScript 3 does not support eval() as some languages do.

There are some bytecode generators such as the ActionScript 3 Eval Library and as3scriptinglib which accomplish similar real time interpretation of code.

As noted, you could leverage a function rest parameter:

public function myFunc(... args) {
    for each (var arg:String in args) {
        trace(arg);
    }
}

Called as:

myFunc("para1");
myFunc("para1", "para2");

Or, use a Function object passing an array of parameters using apply(), called as:

var f:Function = myFunc;
f.apply(this, ["para1", "para2"]);

Or, pass parameters using call(), called as:

var f:Function = myFunc;
f.call(this, "para1", "para2");

Another approach would be creating a Domain Specific Language to be parsed; or, simply parsing the string value as ActionScript such as:

package {

    public class DSL {

        public static function parse(thisArg:*, statement:String) {
            // get the function name
            var fn:String = statement.substr(0, statement.indexOf('('));

            // get parameters (between parenthesis)
            var params:Array = statement.substring(statement.indexOf('(') + 1, statement.lastIndexOf(')')).split(',');
            params.forEach(function(element:*, index:int, arr:Array):void {
                arr[index] = this[element.replace(/^\s+|\s+$/g, '')];
            }, thisArg);

            // execute
            var f:Function = thisArg[fn];
            f.apply(thisArg, params);
        }
    }
}

As a demo, call DSL.parse() and pass a string referencing a function, and properties:

package {

    public class Demo {

        public var para1:String = "hello";
        public var para2:String = "world";

        public function myFunc(... args) {
            for each (var arg:String in args) {
                trace(arg);
            }
        }

        public function Demo() {
            DSL.parse(this, "myFunc(para1);");
            DSL.parse(this, "myFunc(para1, para2);");
        }
    }
}

This would output:

hello

hello
world

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.