-
Function object
So eval() in Flash doesn't work the same as in JavaScript. Fair enough. Can someone please explain to me why the following code doesn't work in Flash (MX)?
PHP Code:
var executors = [];
function exec(func, args) {
if (!executors[args.length]) {
var body, a;
body = "return func(";
for (a = 0 ; a < args.length ; ++a) {
if (a > 0)
body += ", ";
body += "args[" + a + "]";
}
body += ")";
executors[args.length] = new Function("func", "args", body);
}
executors[args.length](func, args);
}
function testFunc(msg1, msg2) {
trace("msg1: " + msg1 + "; msg2: " + msg2);
}
exec(testFunc,["foo","bar"]);
That code should write "msg1: foo; msg2: bar" to the message window. Instead it just sits there and kind of fiddles with its hair. My guess is that the parser won't interpret the function body.
Any ideas on why this doesn't work in AS, but does in JS?
-
you cant create new functions in flash with the new keyword and the function object as a constructor (at least not that Im aware of)... what it seems like you are trying to do is already available to you through the apply method:
testFunc.apply(null, ["foo","bar"]);