home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Book HomeActionScript: The Definitive GuideSearch this book

6.3. The ActionScript Statements

Now that you know how a typical statement is formed, skim Table 6-1 to acquaint yourself with some of the things that ActionScript statements do.

Table 6-1. The ActionScript Statements

Statement

Syntax

Use

break

break;

Cancels a loop

call

call (frame);

Executes the script on a remote frame

continue

continue;

Restarts the current loop

do-while

do {
   statements
} while (expression);

A variation of a while loop

empty statement

;

Holds a place where a statement is expected, and used with evaluate in novice mode

for

for (init; test; increment) {
   statements
}

Executes some code repetitively (a for loop)

for-in

for (property in object) {
   statements
}

Enumerates the properties of an object

function

function name(parameters) {
   statements
}

Declares a function

if-else if-else

if (expression) {
   statements
} else if (expression) {
   statements
} else {
   statements
}

Executes some code based on a condition or a series of conditions

ifFrameLoaded

ifFrameLoaded (frame) {
   statements
}

Executes some code if a certain frame has loaded; deprecated in Flash 5

return

return;
return expression;

Exits a function or returns a value from a function

set

set (variable, value);

Assigns a value to a dynamically named variable

var

var variableName;
var variableName = expression;

Declares and optionally initializes a variable

while

while (expression) {
   statements
}

Executes some code repetitively (a while loop)

with

with (objectName) {
   statements
}

Executes some code in the context of a given object

6.3.9. The with Statement

The with statement provides a shorthand way to refer to properties of an object without having to retype the object's name repeatedly. A with statement takes the general form:

with (object) {
  statements
}

When a property is referenced within a with statement block, object is checked for the specified property. If the property exists in object, then object's property is used to resolve the property reference. If the property does not exist in object, the current timeline or function is consulted for the property in question.

The following example shows the difference between executing a statement inside a with statement and outside a with statement:

PI = 10;                  // Set a timeline variable, PI
with (Math) {             // Execute statements in the context of Math
  trace("pi is: " + PI);  // Displays: 3.1459... (PI is a property of Math)
}
trace("PI is: " + PI);    // Displays: 10 (Math is no longer accessed)

In addition to providing convenient access to object properties, with can be used to invoke object methods:

x = 10;
y = 11;
with (Math) {
  larger = max(x, y);
}
trace(larger);  // Displays: 11

It is not possible to define a new property on an object that is the target of a with statement. Notice in the previous example that the variable larger is not defined on the Math object, so the property reference affects the timeline or function that contains the with statement. The following code shows a misguided attempt to set a variable in myClip:

with (myClip) {
  var x = 10;  // x is set on the current timeline, not myClip
}

We can, however, legitimately use with to affect movie clip instances in other ways. It can provide a handy way to work with deeply nested instance structures. For example, we can change this:

_root.form.userProfile.userID = "U346BX";
  _root.form.userProfile.gotoAndPlay("questionnaire");

to this:

with (_root.form.userProfile) {
  userID = "U346BX";             // Resets an existing variable
                                 // in userProfile instance
  gotoAndPlay("questionnaire");  // Function applied to userProfile instance
}

But with is not our only means of achieving this convenience. We could also simply assign our instance to a variable and use that variable in our references:

var userForm = _root.form.userProfile;
userForm.useriD = "U346BX";
userForm.gotoAndPlay("questionnaire");

Many developers find the variable approach easier to read and work with, but both are valid. We'll learn more about treating movie clips as objects in Chapter 13, "Movie Clips".



Library Navigation Links

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







??????????????@Mail.ru