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.
f2富二代官网入口