Author: Ashwinee Kumar Dash.
Name of the Book: Essential ActionScript 3.0 by Colin Moock
Copyright: 
Disclaimer: Anything in quotation marks is a direct quotation from Essential
ActionScript 3.0. All other notes are my own summaries of the concepts
presented in the book.
These notes are for reference purpose only and not intended to replace the book .Therefore I would strongly advise you to read the original book as well as make your own notes wherever necessary.
Send your suggestions and feedbacks to aswhineedash[at]gmail[dot]com or aswhinee2004[at]gamil[dot]com.
Classes
- Classes are the blue prints upon which objects are built.
- Every object that you see in a program has a class of its own.
- Classes written from scratch known as custom classes.
- Built in classes are used to perform some fundamental tasks
- Classes built directly into ActionScript are known as native classes.
- Classes are created by using class keyword.
- class Identifier {
}
- Name of the class should start with capital letter. Class name is known as Indentifier.
- Class files should be saved with .as extension and the file name should match the class name.
- All classes should reside in a package definition. A folder in that name should be created to contain the .as files.
- To avoid a class conflicting with another class having the same name, it is defined within a package.
- Package names start with small letters to distinguish from class names.
- In ActionScript all program instructions are called directives.
- A package definition can span multiple files but a class definition cannot span multiple files
- Class in AS can have 4 attributes public, private, internal and static. The abstract property is not supported.
- The main class must have public as its attribute set.
- Internal is the default attribute.
- If a class is meant to be used outside its package, it must be declared as public.
- Internal means the class can be used inside the package only.
- objects can be created out of classes by calling its constructor methods
- If no constructor is provided, AS creates a default constructor. In this case no initialization takes place on new instances.
- AS considers all constructor methods as public.
- Codes in a given package can access the classes in that package only.
- Code in a given package can refer to the classes in that package by their unqualified name i.e. name without their package name like com. className.
- To use classes in other packages it must be imported first.
- Once the class has been imported, it can be referred by its unqualified name.
- Packages that have no name associated with them are placed in an automated created package Unnamed Package.These classes can be used in any where in the program without import directive.
- Every object is considered as a self contained, single piece of data known as value.
- Other legal values are null and undefined, they represent the concept of no value.
- A variable is an identifier that refers to a value.
- There are four types of variables: local variables, instance variables, dynamic instance variables and static variables
- // Local variables//
- Local variables track information temporarily within the confinement of a constructor method, an instance method, a static method or a function.
- The variable definition starts with var keyword.
- (Note) any directive that do not include a block statement ends with a semicolumn;
- Value to a variable is assigned by using = sign.Both the = sign and value are known as variable initialiser.
- When variable initialiser is missing actionscript assigns a default value.
- The local variable expires once the function or the method that contains its definition is finished executing.
- // Instance Variable//
- Each object has got some characteristics that describe some aspect of that object. To keep track of an object’s characteristics, instance variables are used.
- Instance variables are created using var keyword directly inside the class body. Once declared, it is directly attached to the instances automatically.
- Instance variables can be and commonly set outside the class.
- Four types of access control modifiers are available for instance variables. They are: public, internal, protected and private.
- Instance variables having public attributes can be accessed both inside and outside of the package.
- Instance variable declared internal can be accessed inside the package only. Internal is the default one.
- Protected instance variables and private instance variables can be accessed by those classes where they have been defined. But the difference lies in subclasses accessing protected ones.
- // constructor parameter //
- Constructor parameters are variables that are declared and used as part of the construction method.
- Assigning values to constructor parameters is known as passing values to variables.
- Required parameter is the parameter whose value has not been initialized
- At the instantiation of the class the required parameters must be passed.
- An expression that contains variable’s name only is known as identifier expression
- Within the body of the constructor method, this refers to the object created i.e. itself.
- // Copies and references//
- When the source variable is an instance of String, Boolean, int, uint, Number, ActionScript makes a copy of that variable and assigns the copy to the destination variable. In other cases it only refers to the original value.
- Instance methods are functions that defines the actions of the object
- Call expression is used to make the object perform the action.
- Like instance variables, instance methods also got access control modifiers. They are public, private, protected and internal
- As usual internal is the default one and it will be assigned unless anything else is specified. Internal is for package use only.
- Public means it can be used outside the package.
- Protected instance methods can be used inside the class and its subclasses only
- Methods accepts values through their parameters
- Methods also return values.
- Method signatures include each parameter’s datatype and return type.




3 comments
Comments feed for this article
October 28, 2007 at 6:34 pm
libyano
Hi …
i need to ask about this point exactly because i have the book and i did n’t understand it
(Within the body of the constructor method, this refers to the object created i.e. itself.)
which object refer to instanse varible? and when we use this.pet.eat(); which object we refer to?
Thanks
October 29, 2007 at 1:33 pm
The Fact
What’s not to understand? Well i won’t try to explain it myself, just afraid to be wrong i guess. Looking forward for the explanation.
November 1, 2007 at 7:53 am
flnotes
Here is how Moock explains it in his book.
“for assigning an instance variable a new value, we
need to start by referring to an object. In this case, we use the local variable pet to
refer to the desired VirtualPet instance:
pet
Next, we write a dot:
pet.
Then, we write the name of the instance variable whose value we wish to assign—in
this case, petName:
pet.petName
Finally, we write an equals sign, then the value we wish to assign to the instance variable.
Let’s use “Stan”:
pet.petName = “Stan”"
“we need to start our variable assignment by
referring to an object. In this case, that object is the new VirtualPet instance being
created. To refer to it, we use the keyword this, which is an automatically created parameter whose value is the object being created: this Within the body of a constructor method, the object being created is known as the current object. To refer to the current object, we use the keyword this.
After the keyword this, we write a dot, followed by the name of the instance variable whose value we wish to assign—in this case petName.
this.petName”
“Finally, we write an equals sign, then the value we wish to assign to the instance
variable:
this.petName = value
The value we wish to assign is the value associated with the name parameter. Hence,for value, we write simply: name.
this.petName = name
At runtime, ActionScript replaces name, in the preceding code, with the value passed to the VirtualPet constructor. That value is then assigned to the instance variable petName.”
“Even though the eat( ) method body does not yet contain any code, with the preceding definition in place, we can already invoke the eat( ) method on a VirtualPet object, as shown in the following updated version of the VirtualZoo class:
package zoo {
public class VirtualZoo {
private var pet;
public function VirtualZoo ( ) {
this.pet = new VirtualPet(“Stan”);
// Invoke eat( ) on the VirtualPet object referenced by the
// variable pet
this.pet.eat( );
}
}
}
Within the eat( ) method body, we want to add 100 to the currentCalories variable of the object through which the eat( ) method was called. To refer to that object, we use the keyword this.
Within the body of an instance method, the object through which the
method is called is known as the current object. To refer to the current
object, we use the keyword this. Notice that the term “current object”
can refer to either the object being created in a constructor method or
the object through which an instance method was called.”
3.1. Omitting the Keyword this
“In Chapter 1, we learned that the keyword this is used to refer to the current object within constructor methods and instance methods. For example, in the following code, the expression this.petName = name tells ActionScript to set the value of the instance variable petName on the object currently being created:
public function VirtualPet (name) {
this.petName = name;
}
Similarly, in the following code, the expression this.currentCalories +=
numberOfCalories tells ActionScript to set the value of the instance variable
currentCalories on the object through which the eat() method was invoked:
public function eat (numberOfCalories) {
this.currentCalories += numberOfCalories;
}
In code that frequently accesses the variables and methods of the current object, including the keyword this can be laborious and can lead to clutter. To reduce labor and improve readability, ActionScript generally allows the current object’s instance variables and instance methods to be accessed without the keyword this.
Here’s how it works: within a constructor method or an instance method, when ActionScript encounters an identifier in an expression, it searches for a local variable, parameter, or nested function whose name matches that identifier. (Nested functions are discussed in Chapter5.) If no local variable, parameter, or nested function’s name matches the identifier, then
ActionScript automatically searches for an instance variable or instance method whose name matches the identifier. If a match is found, then the matching instance variable or instance method is used in the expression.
For example, consider what happens if we remove the keyword this from the eat() method,
as follows:
public function eat (numberOfCalories) {
currentCalories += numberOfCalories;
}
When the preceding method runs, ActionScript encounters numberOfCalories, and tries
to find a local variable, parameter, or nested function by that name. There is a parameter by that name, so its value is used in the expression (in place of numberOfCalories).
Next, ActionScript encounters currentCalories, and tries to find a local variable,parameter, or nested function by that name. No variable, parameter, or nested functionnamed currentCalories is found, so ActionScript then tries to find an instance variable or instance method by that name. This time, ActionScript’s search is successful—the VirtualPet class does have an instance variable named currentCalories, so ActionScript
uses it in the expression. As a result, the value of numberOfCalories is added to the instance variable currentCalories.
Therefore, within the eat() method, the expression this.currentCalories and
currentCalories are identical.
For the sake of easier reading, many developers (and this book) avoid redundant uses of this. From now on, we’ll omit this when referring to instance variables and instance methods. However, some programmers prefer to always use this, simply to distinguish instance variables and instance methods from local variables.
Note that use of the this keyword is legal within instance methods, constructor methods, functions, and code in the global scope only.
Elsewhere, use of the keyword this generates a compile-time error.
NOTE
The process ActionScript follows to look up identifiers is known as identifier
resolution. As discussed in Chapter 16, identifiers are resolved based on the region
(or scope) of the program in which they occur.”
So from the above explanation it is clear that:
var pet = new VirtualPet() means pet is a local variable which refers to the newly created virtual pet object or instance.Thereafter when AS3.0 finds pet, it understands that we are talking about the virtualPet instance.
pet.petName = “Stan”
Here petName is an instance variable (not a local variable because it is defined in the VirualPet class).
As pet is an instance of VirtualPet, it can access all the instance variables defined in the class. So petName variable of pet is assigned the value “Stan”.
this.pet.petname = “Stan”
In this case “this” keyword means the current object. Here the current object is the virtulPet object referred to by variable pet. And it is same as pet.petname = “Stan” .
AS 3.0 is now intelligent enough to find the object we are reffering without the help of keyword “this”. So you can avoid using it in such cases “when referring to instance variables and instance methods”.