O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Expressions

The written form of a value in an ActionScript program is known as an expression. For example, the following code shows a new expression—an expression representing a new object (in this case, a Date object):

new Date()

Likewise, the following code shows a literal expression representing a Number object with the value 2.5:

2.5

Individual expressions can be combined together with operators to create a compound expression, whose value is calculated when the program runs. An operator is a built-in command that combines, manipulates, or transforms values (which are known as the operator's operands). Each operator is written using either a symbol, such as +, or a keyword, such as instanceof.

For example, the multiplication operator, which multiplies two numbers, is written using the asterisk symbol (*). The following code shows a compound expression that multiplies 4 and 2.5:

4 * 2.5

When the preceding code is executed, ActionScript calculates the result of multiplying 4 by 2.5, and the entire compound expression (4 * 2.5) is replaced by that single calculated result (10). Calculating the value of an expression is known as evaluating the expression.

Tip

For a complete list of ActionScript operators, see Chapter 10.

To represent values that are not known when a program is compiled (at compile-time), but are supplied or calculated when the program runs (i.e., at runtime), we use variable names. When ActionScript evaluates an expression containing a variable name, it replaces that variable name with the corresponding variable's value. The process of replacing the variable name with the variable's value is known as retrieving, getting, or reading the variable value.

For example, consider the following compound expression, in which two values represented by variable names are multiplied together:

quantity * price

The variables quantity and price are placeholders for values that will be determined at runtime. The value of quantity might be, say, a number supplied by the user, while the value of price might be a number retrieved from a database. For the sake of this example, let's assume that the variable quantity has the value 2, and the variable price has the value 4.99.

When ActionScript evaluates the expression quantity * price, it replaces quantity with 2 and price with 4.99. Hence, during evaluation, the expression reads:

2 * 4.99

And the final value of the expression is:

9.98

Tip

In formal terms, an expression that contains a variable name only, such as quantity, is known as an identifier expression.

Now let's try using an identifier expression in our virtual pet program.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required