Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Dustin's Software Development Cogitations and Speculations

This blog is about my observations and thoughts related to software development. These observations include tips and tricks that I have learned, solutions to problems I have faced, and other concepts I have found interesting and useful. This blog is intended to provide information to help other developers facing the same issues as well as providing me a method to document things in a well-known location for my own future reference.


No Block Scope in ActionScript

 

One of the things that I most liked about ActionScript when I first began to use it was how easy it was to pick up after using Java for several years. I have blogged about some of the similarities Java and ActionScript share, but there are also some differences (such as ActionScript's support for switching on Strings). In this brief blog entry, I will demonstrate one subtle difference in behavior in ActionScript from what I am used to from years of C++ and Java experience.

While ActionScript 3.0 supports global variables and constants and also supports variables and constants local to a function, it does not support block scoping. The "Variables" section of Chapter 4 ("ActionScript Language and Syntax") of Programming ActionScript 3 describes this in more detail. In this blog entry, I'll demonstrate this with a brief example.

VariableScopeTest.mxml

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
width="900" height="900"
applicationComplete="demonstrateVariableScope()">
<mx:Script>
import mx.controls.Alert;
const aConstant:String = "GlobalConstant";
var aVariable:String = "GlobalVariable";
private function demonstrateVariableScope():void
{
const firstConstant:String = "firstConstant";
var firstVariable:String = "firstVariable";
const aConstant:String = "LocalConstant";
var aVariable:String = "LocalVariable";
if ( 1 )
{
const secondConstant:String = "secondConstant";
var secondVariable:String = "secondVariable";
}
Alert.show(
"First Constant: " + firstConstant + "\n"
+ "First Variable: " + firstVariable + "\n"
+ "Second Constant: " + secondConstant + "\n"
+ "Second Variable: " + secondVariable + "\n"
+ "Global/Local Constant: " + aConstant + "\n"
+ "Global/Local Variable: " + aVariable + "\n" );
}
</mx:Script>
</mx:Application>

The output from running the SWF that results from compiling the above code looks like this:

This example demonstrates a couple important points about variable and constant scoping in ActionScript:

1. Like Java, local variables (variables local to a function) override variables of the same name on a more global level.

2. Unlike Java, ActionScript variables defined within a block are actually scoped to the entire function containing that block rather than to simply the block itself.

Where something like this can cause a little trouble is when a developer tries to use the same constant name in two different blocks. This will result in a compiler error ("Error: A conflict exists with definition ...") because the compiler will see the constants declared in two separate blocks within the same function as being an attempt to redefine a constant.

This is not a huge deal, but it is a subtlety that can be a surprise to a Java developer learning ActionScript.

What is Tech Briefcase?
TechBriefcase is a new, free service where IT Professionals can Search, Store and Share IT white papers and content like this. Learn more
Bookmark content
Speed up your research efforts with content across the web.
Search and Store
Find the white papers you need. Create folders for any topic.
View Anywhere
Open your briefcase on your iPhone, tablet or desktop. Share with colleagues.
Don't have an account yet?