Book HomeActionScript: The Definitive GuideSearch this book

3.5. Primitive Data Versus Composite Data

So far we've been working mostly with numbers and strings, which are the most common primitive datatypes. Primitive datatypes are the basic units of a language; each primitive value contains a single datum (as opposed to an array of multiple items) and describes that datum literally. Primitive data is very straightforward.

ActionScript supports these primitive datatypes: number, string, boolean, undefined, and null. ActionScript does not have a separate single-character datatype (i.e., char) as found in C/C++.

Primitive datatypes are, as their name suggests, simple. They can hold text messages, frame numbers, movie clip size values, and so on, but they don't readily accommodate higher levels of complexity. For more elaborate data handling -- such as simulating the physics of a dozen bouncing balls or managing a quiz with 500 questions and answers -- we turn to composite datatypes. Using composite data, we can manage multiple pieces of related data as a single datum.

ActionScript supports the following composite datatypes: array, object, and movieclip. Functions are technically a type of object and are therefore considered composite data, but we rarely manipulate them as such. See Chapter 9, "Functions", for more about functions as a datatype.

Whereas a single number is a primitive datum, a list (i.e., an array) of multiple numbers is a composite datum. Here's a practical example of how composite datatypes are useful: Suppose we wanted to track the profile of a customer named Derek. We could create a series of variables that store Derek's attributes as primitive values, like this:

var custName = "Derek";
var custTitle = "Coding Genius";
var custAge = 30;
var custPhone = "416-222-3333";

However, this format gets pretty cumbersome once we add even a few more customers. We're forced to use sequentially named variables to keep track of everything -- cust1Name, cust2Name, cust1Title, cust2Title, and so on. Yuck! But if we use an array, we can store our information much more efficiently:

cust1 = ["Derek", "Coding Genius", 30, "416-222-3333"];

When we want to add more customers we just create new arrays:

cust2 = ["Komlos", "Comic Artist", 28, "515-515-3333"];
cust3 = ["Porter", "Chef", 51, "515-999-3333"];

Nice and tidy. We'll learn much more about composite datatypes in the coming chapters.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.