Table of Contents

3.3 Creating and Categorizing Data

There are two ways to create a new datum with ActionScript, and both methods require the use of expressions—phrases of code that represent data in scripts.

A literal expression (or literal for short) is a series of letters, numbers, and punctuation that is the datum. A data literal is a verbatim description of data in a program's source code. This contrasts with a variable, which is merely a container that holds a datum. Each datatype defines its own rules for the creation of literals. For example, string literals are enclosed in quotes, whereas numeric literals are not. Here are some examples of literals:

"loading...please wait"  // A string literal
1.51                     // A numeric literal
["jane", "jonathan"]     // An array literal
{x: 10, y: 15}           // An object literal

Note that movie clips cannot be represented by literals but are referred to by instance names.

We can also generate data programmatically with a complex expression. Complex expressions are phrases of code with a value that must be calculated or computed, not taken literally. The calculated value is the datum being represented. For example, each of these complex expressions results in a single datum:

1999 + 1       // Yields the numeric datum 2000
"1999" + "1"   // Yields the string datum "19991"
"hi " + "ma!"  // Yields the string datum "hi ma!"
firstName      // Yields the value of the variable firstName
_currentframe  // Yields the frame number of the playhead's current position
new Date()     // Yields a new Date object with the current date and time

Notice that an individual literal expression, such as 1999 or 1, can be a valid part of a larger complex expression, as in 1999 + 1.

Whether we use a literal expression or a complex expression to create data, we must store every datum that we want to use later. The result of the expression "hi" + "ma!" is lost unless we store it, say, in a variable. For example:

// This datum is fleeting and dies immediately after it's created
"hi " + "ma";
// This datum is stored in a variable and can be
// accessed later via the variable welcomeMessage
var welcomeMessage = "hi " + "ma!";

How do we categorize data into the appropriate type? That is, how do we specify that a datum is a number, a string, an array, or whatever? In most cases, we don't categorize new data ourselves; the ActionScript interpreter automatically assigns or infers each datum's type based on a set of internal rules.

3.3.1 Automatic Literal Typing

The interpreter infers a literal datum's type by examining its syntax, as explained in the comments in the following code fragment:

"animal"              // Quotation marks identify "animal" as a string
1.35                  // If it contains only integers and a decimal point,
                      // it is a number
true                  // Special keyword true identifies this as a Boolean
null                  // Special keyword null identifies this as the null type
undefined             // Special keyword undefined identifies the undefined type
["hello", 2, true]    // Square brackets and values separated by commas 
                      // indicate that this is an array
{x: 234, y: 456}      // Curly braces and property name/value pairs separated
                      // by commas indicate that this is an object

As you can see, using correct syntax with data literals is extremely important. Incorrect syntax may cause an error or result in the misinterpretation of a datum's content. For example:

animal   // Missing quotes--animal is interpreted as a variable,
         // not a string of text
"1.35"   // Numbers in quotes are treated as strings, not numbers
1. 35    // Space before the 3 causes an error
"animal  // Missing closing quotation mark causes an error

3.3.2 Automatic Complex Expression Typing

The interpreter computes an expression's value in order to determine its datatype. Consider this example:

pointerX = _xmouse;

Because _xmouse stores the location of the mouse pointer as a number, the type of the expression _xmouse will always be a number; so, the variable pointerX also becomes a number.

Usually, the datatype that is automatically determined by the interpreter matches what we expect and want. However, some ambiguous cases require us to understand the rules that the interpreter uses to determine an expression's datatype (see Example 2-2 and Example 2-3). Consider the following expression:

"1" + 2;

The operand on the left of the + is a string ("1"), but the operand on the right is a number (2). The + operator works on both numbers (addition) and strings (concatenation). Should the value of the expression "1" + 2 be the number 3 or the string "12"? To resolve the ambiguity, the interpreter relies on a fixed rule: the plus operator (+) always favors strings over numbers, so the expression "1" + 2 evaluates to the string "12", not the number 3. This rule is arbitrary, but it provides a consistent way to interpret the code. The rule was chosen with typical uses of the plus operator in mind: if one of the operands is a string, it's likely that we want to concatenate the operands, not add them numerically, as in this case:

trace ("The value of x is: " + x);

Combining disparate types of data or using a datum in a context that does not match the expected datatype causes ambiguity. This forces the interpreter to perform an automatic datatype conversion according to arbitrary, but predictable, rules. Let's examine the cases in which automatic conversions will occur and the results of converting a datum from one type to another.


Table of Contents