Macromedia flash media server 2-developing media applications User Manual

Page of 114
104
Application Development Tips and Tricks
Initializing applications
Initialization of an application is used to set the starting state. It should be the first function 
call in the application. This function should be the only call for initialization made in your 
program; all other calls should be event driven.
// frame 1
this.init();
function init()
{
if (this.inited != undefined)
return;
this.inited = true;
// Initialization code here
}
Using var for local variables
All local variables should use the keyword 
var
. This prevents variables from being accessed 
globally and, more importantly, prevents variables from being inadvertently overwritten. 
Using 
var
 also lets you use strict typing for an object for type checking at compile time. For 
more information on strict typing, search for “strict data typing” in Flash Help.
For example, the following code does not use the keyword 
var
 to declare the variable and 
inadvertently overwrites another variable.
counter = 7;
function loopTest(){
trace(counter);
for(counter = 0; counter < 5; counter++){
trace(counter);
}
}
trace(counter);
loopTest();
trace(counter);
This code outputs the following:
7
7
0
1
2
3
4
5