Use of eval in Flash

Sometimes I find programming Flash so frustrating. It seems to break convention in a number of areas that it often leaves me banging my head against my desk for hours. My recent beef: the use of "eval". In programming with javascript, I've enjoyed being able to use eval to evaluate a full expression. For example, eval("myimg.height + 20") would determine the height of my image and then add 20 to it. In Flash MX, the eval function can't evaluate an expression or a number. A couple examples: eval("Stage.height") will work but eval("Stage.height + 20") will not. Even eval("20") gives undefined. I'm hoping this has changed in MX 2004 or I'm going to have a very sore head!

Published November 12, 2003 · Updated September 17, 2005
Categorized as Other
Short URL: https://snook.ca/s/93

Conversation

14 Comments · RSS feed
roneil said on February 19, 2004

I have been experiencing a similiar behavior in 2004. Somtimes the eval() works other time it does not. In 2004 they recommend using the _root[] notation but I am having similiar results. If anyone has a consistent working solution to evaluating a full expression I would love to know it

moke said on July 13, 2004

Same goes for me, eval function sometimes works sometimes does not in professional 2004. Does anyone have any other solution ... I thought I could depend on this function .. I thought wrong.

paSipHIsT said on February 21, 2005

I'm having troubles with eval and this also. I've tried using actual examples out of the docs and they don't work either. I think there is something seriously wrong with MX 2004. It's very frustrating; I can't think of a cludge.

paSipHIsT said on February 21, 2005

Hey guys, I think I've figured it out.

I tried a few tests. The first thing I did was this:

var TextField1;
this["TextField1"] = 12;
trace ("TextField1 = " + TextField1);

TextField1 traced to equallying to 12. excellent. That's what I wanted. Then I tried this:

function testthis() {
var TextField1;
this["TextField1"] = 12;
trace ("TextField1 = " + TextField1);
}
testthis();

TextField1 traced to "undefined". Crap. That's no good. Then I tried this:

var TextField2;
function testthis2() {
this["TextField2"] = 12;
trace ("TextField2 = " + TextField2);
}
testthis2();

This worked! Note the variable declaration is OUTSIDE the function that I use the variable in? It seems as if that's want needs to happen. Hope that helps.

Jonathan said on February 21, 2005

paSipHIsT: The problem you're having has to do with variable scope. This is definitely a topic that needs to be understood well to ensure that you're accessing variables or objects properly.

paSipHIsT said on February 21, 2005

Jonathan:

I'm not sure what you mean to say. Are you telling me that the issue I was having (in the second example) was a scope problem (on my part)? I don't think so. I declare the variable in the function, before I use it in the same function. That should work.

Jonathan Snook said on February 21, 2005

The problem is in how you are referencing your variables and assigning them values. It's creating wierd behaviour related to scope.

The problem is that you are using this which allows you to reference an object within a particular context. If you trace(this) either in the root or in a function sitting on the root, you'll notice that it references _level0. This is important when we look back at your code.

In your second example you declare a variable inside the function. Then you try to use this to reference the variable but there's no TextField1 variable on _level0, just the one in your function. So, when you go to trace out the value of your variable it returns undefined because you haven't assigned it a value yet.

Claudiu said on November 18, 2005

Well, sadly this is the expected behaviour. I quote below some lines of the Macromedia Flash MX Professional documentation:

"Some of the differences between ActionScript and JavaScript are described in the following list:

ActionScript does not support browser-specific objects such as Document, Window, and Anchor.
ActionScript does not completely support all the JavaScript built-in objects.
ActionScript does not support some JavaScript syntax constructs, such as statement labels.
In ActionScript, the eval() function can perform only variable references. " (...)

That is: You can use eval to obtain variables, but not to compute anything

Glen Pawson said on May 24, 2006

http://www.adobe.com/cfusion/knowledgebase/index.cfm?id=tn_16187

Isn't it lovely :D

jsrpg said on August 19, 2006

Jonathan,i tried to execute a ActionScript code as string format.
How to do it?

example:

var str="var t=0;t++;tarce(t);";

eval(str);//it's wrong!

Please tell me.thank you!

Jonathan Snook said on August 19, 2006

jsrpg: take a look at Mark Wubben's alternative. I haven't tried it myself but it might do the trick for you.

jsrpg said on August 20, 2006

Ah,it isn't answer my question.it just return a value,but i need to execute the string-code.
The effect just like the eval() function in javascript.

Robin Sernhede said on September 19, 2006

_root["stage"]["height"] += 5;

Simplicus said on October 17, 2007

If you want full (JavaScript type) functionality of the eval function in ActionScript 3, use the ExternalInterface class to call a javascript function in the html wrapper, thus:

Actionscript code:


...
if(ExternalInterface.available) {
   num1 = String(ExternalInterface.call("parseStr", Answer.text));
   Answer.text = num1;
}
else {
   Answer.text = "Function Unknown";
}

//...and the javascript to add to the html wrapper:

  function parseStr(str)
  {
     var result = eval(str);
     return(result);
  }
//-->

Note that in my implementation, the javascript in the container html file is in its own <script...> block. Also, my implentation assumes AS3, and was tested in Flex 2.

Sorry, comments are closed for this post. If you have any further questions or comments, feel free to send them to me directly.