2. Object-Oriented Programming Language
◦ Encapsulation
◦ Abstraction
◦ Inheritance
◦ Polymorphism
ECMA compliant & procedural language
ActionScript code is defined in files with the .as extension or embedded
within MXML files using one of the following methods:
◦ Embed ActionScript code in mxml files using the tag <mx:Script>
◦ Include external ActionScript files into mxml, for example : <mx:Script
source=”calculateTax.as”>
◦ Import ActionScript classes
Angelin
3. ActionScript variables are strongly typed.
Syntax: var varName:VarType = new VarType();
ActionScript variable definitions:
◦ var object:Object = new Object();
◦ var array:Array = new Array();
◦ var count:int = 1; // short-hand int definition
◦ var name:String = "Donnie Brasco"; // short-hand string definition
◦ var names:Array = { "Alice", "Bob", "Carl" }; // short-hand array definition
ActionScript does support type-less variables, but it is generally not
encouraged.
◦ var var1 = null; // un-typed variable
◦ var var2:* = null; // any-typed variable
ActionScript also supports using "*" (wildcard) as a variables.
◦ var varName:*;
The "*" wildcard type means any object type can be assigned to the variable.
Angelin
4. int - Represents a 32-bit signed integer.
Number - Represents an IEEE-754 double-precision floating-point number.
Boolean - can have one of two values, either true or false, used for logical
operations.
String - Represents a string of characters.
Date – Represents date and time information.
Array – To access and manipulate arrays.
XML - The XML class contains methods and properties for working with XML objects.
XMLList - The XMLList class contains methods for working with one or more XML
elements.
uint – A class that provides methods for working with a data type representing a 32-
bit unsigned integer.
Error - Contains information about an error that occurred in a script.
RegExp - To work with regular expressions, which are patterns that can be used to
perform searches in strings and to replace text in strings.
Angelin
5. ActionScript properties can be declared by using five different access
modifiers:
◦ internal: Makes the property visible to references inside the same package
(the default modifier)
◦ private: Makes the property visible to references only in the same class
◦ protected: Makes the property visible to references in the same class and
derived classes
◦ public: Makes the property visible to references everywhere
◦ static: Makes the property accessible via the parent class instead of
instances of the class
Angelin
6. An ActionScript class must define a public constructor method, which
initializes an instance of the class.
A constructor has the following characteristics:
◦ No return type.
◦ Should be declared public.
◦ Might have optional arguments.
◦ Cannot have any required arguments if you use it as an MXML tag.
◦ Calls the super() method to invoke the superclass‟s constructor.
The super() method is called from within the constructor which invokes
the superclass‟s constructor to initialize the items inherited from the
superclass. The super() method should be the first statement in the
constructor;
Angelin
7. Function Definition syntax:
access_modifier function functionName(argName:ArgType,
optionalArgName:optionalArgType=defaultValue):ReturnType {
Statement1;
..
StatementN;
}
Invoking a function
var returnValue:ReturnType = functionName(argName);
Examples:
public function doSomething( arg:Object ):String
{
return "something";
}
//function with an optional argument
public function anotherFunction( arg:Object, opt:String=null ):void
{ // do something
}
Angelin
8. Events
Actions that occur in response to user-initiated and system-initiated actions
Event Handling
The technique for specifying certain actions that should be performed in
response to particular events.
Example
function eventHandler(event:MouseEvent):void
{
// Actions performed in response to the event go here
}
myButton.addEventListener(MouseEvent.CLICK, eventHandler);
Angelin
9. ActionScript class definition:
package packageName
{
import another.packageName.*;
public class ClassName extends SuperClassName implements
InterfaceName
{
public ClassName()
{
// constructor
}
}
}
Angelin
10. ActionScript interface definition:
package packageName
{
public interface InterfaceName extends AnotherInterface
{
// public not necessary (and not allowed)
function methodName():void;
}
}
ActionScript Interfaces do not allow property definitions.
Angelin
11. Runtime Type Checking is done using the „is‟ operator.
Casting of objects is done using the „as‟ operator.
The ActionScript „as‟ operator will cast the object and assign it to
the variable, but only if object is of the proper type (otherwise it
assigns null).
ActionScript usage of the „as‟ operator:
// use "as" to safely assign object as a Person or null
var person:Person = object as Person;
ActionScript runtime type checking and casting example:
// cast Object to Person
if( object is Person )
{
var person:Person = Person ( object );
}
Angelin
12. ActionScript "for" and "for each" loops:
// “for” loop to loop over an array
for( var i:int = 0; i < array.length; i++ )
{
// use array[i]
}
// "for each" loop to loop over a collection
var item:Object;
for each( item in collection )
{
// use item
}
Angelin
13. ActionScript throws exceptions and supports the try/catch/finally structure
for handling them.
ActionScript exception handling using try/catch/finally:
// functions do not declare that they throw exception
public function doSomething():void
{
try {
// try something
}
catch( error:Error ) {
// handle error by rethrowing
throw error;
}
finally {
// executed whether or not there was an exception
}
}
Angelin
15. Java class definition ActionScript class definition
package packageName; package packageName
{
import another.packageName.*; import another.packageName.*;
public class ClassName extends public class ClassName extends
SuperClassName implements SuperClassName implements
InterfaceName InterfaceName
{ {
public ClassName() public ClassName()
{ {
// constructor // constructor
} }
} }
}
Angelin
16. Java interface definition ActionScript interface definition
package packageName; package packageName
{
public interface InterfaceName extends public interface InterfaceName extends
AnotherInterface AnotherInterface
{ {
// public is optional (and // public not necessary (and not
extraneous) allowed)
public void methodName(); function methodName():void;
} }
}
Unlike Java, ActionScript Interfaces do not
allow property definitions.
Angelin
17. Java variable definitions ActionScript variable definitions
Object object = new Object(); var object:Object = new Object();
int count = 1; var count:int = 1;
String name = “Don Bradman"; var name:String = "Don Bradman";
String[] names = { "Alice", "Bob", var names:Array = { "Alice", "Bob", "Carl" };
"Carl" }; // ActionScript arrays are untyped
ActionScript does support type-less
variables, but it is generally not encouraged.
var var1 = null; // un-typed variable
var var2:* = null; // any-typed variable
Angelin
18. Java method definition ActionScript method definition
public String doSomething( Object public function doSomething( arg:Object
arg ) ):String
{ {
return "something"; return "something";
} }
ActionScript also allows optional function
arguments.
ActionScript function definition with optional
function argument:
public function anotherFunction( arg:Object,
opt:String=null ):void
{
// do something
}
Angelin
19. Java runtime type checking and ActionScript runtime type checking and
casting casting
// cast Object to Person // cast Object to Person
if( object instanceof Person ) if( object is Person )
{ {
Person person = (Person) var person:Person = Person( object );
object; }
} For runtime type checking ActionScript uses
the „is‟ operator.
For runtime type checking Java uses
the „instanceof ‟operator ActionScript also offers another method,
similar to casting, using the „as‟ operator.
The ActionScript „as‟ operator will cast the
object and assign it the variable, but only if
object is of the proper type (otherwise it
assigns null).
// use "as" to safely assign object as a
Person or null
var person:Person = object as Person;
Angelin
20. Java "for" and "for each" loops ActionScript "for" and "for each" loops
// loop over an array // loop over an array
for( int i = 0; i < array.length; i++) for( var i:int = 0; i < array.length; i++)
{ {
// use array[i] // use array[i]
} }
// loop over a collection // loop over a collection
for( Object item : myCollection) for each(var item:Object in myCollection)
{ {
// use item // use item
} }
Angelin
21. Java exception handling ActionScript exception handling
// method declared to throw an // functions do not declare that they
exception throw exception
public void doSomething() throws public function doSomething():void
Exception {
{ try
try {
{ // try something
// try something }
} catch( error:Error )
catch( Exception ex ) {
{ // handle error by rethrowing
// handle exception by throw error;
rethrowing }
throw ex; finally
} {
finally // reached whether or not there is
{ an exception
// reached whether or not there }
is an exception }
Angelin
}
}
22. Java Accessor Functions (Get/Set) ActionScript Accessor Functions (Get/Set)
public class Person public class Person
{ {
private String name = null; private var _name:String;
public String getName() public function get name():String
{ {
return this.name; return _name;
} }
public void setName( String name ) public function set name( value:String
{ ):void
this.name = name; {
} _name = value;
} }
}
Angelin
23. Java Singleton Class ActionScript Singleton Class
package examples; package examples
{
public class Singleton public class Singleton
{ {
private static final Singleton _instance static private const _instance:Singleton = new
= new Singleton(); Singleton();
private Singleton() // private constructors not supported
{} public Singleton()
{
public Singleton getInstance() if( _instance )
{ {
return _instance; throw new Error( "Singleton instance already
} exists." );
} }
}
static public function get instance():Singleton
{
return _instance;
}
}
Angelin
}
24. Concept/Language
Java 5.0 ActionScript 3.0
Construct
Class library packaging .jar .swc
class Employee extends class Employee extends
Inheritance
Person{…} Person{…}
var firstName:String=”John”;
String firstName=”John”;
var shipDate:Date=new
Variable declaration and Date shipDate=new Date();
Date();
initialization int i;int a, b=10;
var i:int;var a:int, b:int=10;
double salary;
var salary:Number;
It‟s an equivalent to the wild
card type notation *. If you
Undeclared variables Not Applicable declare a variable but do not
specify its type, the * type will
apply.
If you write one statement per
Terminating statements
Mandatory line you can omit semicolon
with semicolons
at the end of that statement.
Angelin
25. Concept/Language
Java 5.0 ActionScript 3.0
Construct
No block scope.
block: declared within curly local: declared within a
braces, function
local: declared within a method member: declared at the
Variable scopes or a block class level
member: declared at the class global: If a variable is
level declared outside of any
global: no global variables function or class definition,
it has global scope.
Immutable; stores
Immutable; stores sequences of
Strings sequences of two-byte
two-byte Unicode characters
Unicode characters
for strict equality use ===
Strict equality operator Not Applicable for strict non-equality use
!==
The keyword final The keyword const
Constant qualifier
final int STATE=”NY”; const STATE:int =”NY”;
Angelin
26. Concept/Language
Java 5.0 ActionScript 3.0
Construct
The top class in the
Object Object
inheritance tree
Dynamic (checked at run-time) and static
Static (checked at
Type checking (it‟s so called „strict mode‟, which is default
compile time)
in Flex Builder)
is – checks data type.
Type check i.e. if (myVar is String){…}
instanceof
operator The „is‟ operator is a replacement of older
instanceof
Similar to „is‟ operator, but does not return
Boolean, instead returns the result of
expression
The „as‟ operator Not Applicable
var orderId:String=”123″;
var orderIdN:Number=orderId as Number;
trace(orderIdN);//prints 123
Person p=(Person) var p:Person= Person(myObject);
Casting
Angelin
myObject; orvar p:Person= myObject as Person;
27. Concept/Language
Java 5.0 ActionScript 3.0
Construct
All primitives in ActionScript are objects.
byte, int, long,
Boolean, int, uint, Number, String.
float,
Primitives The following lines are equivalent:
double,short,
var age:int = 25;
boolean, char
var age:int = new int(25);
Array, Date, Error, Function, RegExp, XML,
Complex types Not Applicable
and XMLList
var quarterResults:Array
int
=new Array();
quarterResults[];
orvar quarterResults:Array=[];
quarterResults =
Array declaration var quarterResults:Array=
new int[4];
and instantiation [25, 33, 56, 84];
int
AS3 also has associative arrays that uses
quarterResults[]={
named elements instead of numeric indexes
25,33,56,84};
(similar to Hashtable).
var myObject:*;
Un-typed variable Not Applicable
var myObject;
Angelin
28. Concept
/Language Java 5.0 ActionScript 3.0
Construct
package com.xyz{class myClass{…}}
package com.xyz;
packages ActionScript packages can include not only
class myClass {…}
classes, but separate functions as well
public, private, public, private, protected
protected if none is specified, classes have internal
Class access
if none is specified, access level (similar to package access level in
levels
classes have package Java)
access level
Similar to XML namespaces.
Custom access namespace abc;
levels: Not Applicable abc function myCalc(){}
namespaces or
abc::myCalc(){}use namespace abc ;
Angelin
29. Concept
/Language Java 5.0 ActionScript 3.0
Construct
Hashtable, Associative Arrays allows referencing its
MapHashtable friends = elements by names instead of indexes.
new Hashtable(); var friends:Array=new Array();
friends.put(“good”, friends["good"]=”Mary”;
“Mary”); friends["best"]=”Bill”;
friends.put(“best”, friends["bad"]=”Masha”;
Unordered “Bill”); var bestFriend:String= friends["best"];
key-value friends.put(“bad”, friends.best=”Alex”;
pairs “Masha”); Another syntax:
String bestFriend=
var car:Object = {make:”Toyota”,
friends.get(“best”);//
model:”Camry”};
bestFriend is Bill
trace (car["make"], car.model);
// Output: Toyota Camry
Angelin
30. Concept
/Language Java 5.0 ActionScript 3.0
Construct
Console // in debug mode only
System.out.println();
output trace();
import com.abc.*;
import com.abc.*;
import com.abc.MyClass;
imports import
packages must be imported even if the class
com.abc.MyClass;
names are fully qualified in the code.
Compiler moves all variable declarations to
the top of the function. So, you can use a
Hoisting Not Applicable
variable name even before it has been
explicitly declared in the code.
Angelin
31. Concept/Language
Java 5.0 ActionScript 3.0
Construct
Customer cmr = var cmr:Customer = new Customer();
new Customer(); var cls:Class =
Instantiation Class cls = flash.util.getClassByName(“Customer”);
objects from Class.forName(“C var myObj:Object = new cls();
classes ustomer”);Object
myObj=
cls.newInstance();
private class There is no private classes in AS3.
Private classes
myClass{…}
Supported. Not available.
Typical use: Implementation of private constructors is
singleton classes. postponed as they are not the part of the
ECMAScript standard yet.
Private constructors To create a Singleton, use public static
getInstance(), which sets a private flag
instanceExists after the first instantiation.
Angelin
Check this flag in the public constructor, and
if instanceExists==true, throw an error.
32. Concept/Language
Java 5.0 ActionScript 3.0
Construct
A file can have multiple A file can have multiple class
class declarations, but declarations, but only one of them
Class and file only one of them can be can be placed inside the package
names public, and the file must declaration, and the file must have
have the same name as the same name as this class.
this class.
dynamic class Person {var
name:String;}
Dynamic classes //Dynamically add a variable and a
function
(define an object
Person p= new Person();
that can be altered p.name=”Joe”;
at runtime by Not Applicable p.age=25;
adding or changing p.printMe = function () {
properties and
trace (p.name, p.age);
methods).
}
Angelin
p.printMe(); // Joe 25
33. Concept/Language
Java 5.0 ActionScript 3.0
Construct
Not Applicable. myButton.addEventListener(“click”,
Closure is a proposed myMethod);
addition to Java 8. A closure is an object that represents a
snapshot of a function with its lexical
Function closures
context (variable‟s values, objects in the
scope). A function closure can be
passed as an argument and executed
without being a part of any object
Abstract classes Supported Not Applicable
class A implements B class A implements B {…}
{…} Interfaces can contain only function
Interfaces Interfaces can contain declarations.
method declarations
and final variables.
Classes and interfaces Classes, interfaces, variables, functions,
What can be placed
namespaces, and executable
in a package
Angelin
statements.
34. Concept/Language
Java 5.0 ActionScript 3.0
Construct
Supported. You must use the override
Function overriding Supported
qualifier
Function
Supported Not supported.
overloading
Keywords: try, catch, throw, finally
A method does not have to declare
Keywords: try, catch,
exceptions.
throw, finally, throws.
Can throw not only Error objects, but
Exception handling Uncaught exceptions
also numbers. For example,
are propagated to the
throw 25.3;
calling method.
Flash Player terminates the script in
case of any uncaught exception.
Regular
Supported Supported
expressions
Angelin
35. Almost anything that you can do in a custom ActionScript
custom component, you can also do in a custom MXML
component. However, for simple components, such as
components that modify the behaviour of an existing
component or add a basic feature to an existing component,
it is simpler and faster to create them in MXML.
When your new component is a composite component that
contains other components, and you can express the
positions and sizes of those other components using one of
the Flex layout containers, you should use MXML to define
your component.
Angelin
36. To modify the behaviour of the component, such as the way a
container lays out its children, use ActionScript.
To create a visual component by creating a subclass from
UIComponent, use ActionScript.
To create a non-visual component, such as a formatter,
validator, or effect, use ActionScript.
To add logging support to your control, use ActionScript.
Angelin