eval( ) Global Function

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 18.  ActionScript Language Reference
eval( ) Global Function Flash 6

interpret a string as an identifierFlash 4; illegal on left side of assignment in
eval(stringExpression)

Arguments

stringExpression

A string expression that matches some identifier. Can be qualified (e.g., "_root.speed") or unqualified (e.g., "speed").

Returns

The value of the variable represented by stringExpression or a reference to the object, movie clip, or function represented by stringExpression. If stringExpression does not represent an existing identifier, undefined is returned.

Description

The eval( ) function provides a means of dynamically constructing an identifier based on a string of text. It looks up the variable identified by stringExpression and returns its value. For example, here we use the return value of eval( ) to set the value of currentName:

var name1 = "Kathy"; var count = 1; // Look up name1 and return its value, "Kathy", which we assign to currentName var currentName = eval("name" + count);

And here we control a dynamically named movie clip, star1:

var count = 1; eval("star" + count)._x += 100;       // Move star1 right 100 pixels

In Flash 4 and 5 (but not Flash 6), we can also use an eval( ) invocation in place of an identifier that is the left-hand operand of an assignment expression, as in:

var count = 4; eval("name" + count) = "Simone";     // Sets name4 to "Simone" in Flash 4 and 5

However, in Flash 6 eval( ) cannot be used on the left side of an assignment (in keeping with the ECMA-262 specification). To dynamically construct the left-hand side of an assignment expression, use the property access operator, [ ], or the set statement instead:

// Using [  ] var count = 4; this["name" + count] = "Simone";    // Sets name4 to "Simone" trace (name4);     // Using set var count = 4; set("name" + count, "Simone");    // Also sets name4 to "Simone" trace (name4);

Note that, unlike its JavaScript cousin, eval( ) in ActionScript does not allow for the compiling and execution of arbitrary blocks of code in a string. Full support of eval( ) would require an ActionScript compiler in the Player, which would cause too great an increase in the Player size.

Usage

As of Flash 5, eval( ) is rarely needed for dynamic variable access. When managing multiple pieces of data, use arrays and objects instead.

Example

The eval( ) function is often used to convert the MovieClip._droptarget string property or the Selection.getFocus( ) return value to an object reference. In the following example, suppose we have a series of cartoon face clips. When the user drops a food clip onto one of the faces, we retrieve the path to the face in question using _droptarget, and then we use eval( ) to retrieve an object reference to that face. Finally, we send the face to a frame showing the mouth full of food:

// Convert _droptarget string to a reference theFace = eval(food._droptarget); // Control appropriate face clip using converted reference theFace.gotoAndStop("full");

See Also

"Executing Code in a String with eval," in Chapter 4



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net