14
encapsulation

objects

Object is the most general word in actionscript. it is a core datatype, and the base from which all other complex types are constructed. objects are relatively simple, but very flexible. they collect a number of values, but instead of storing them in sequential order like an array, an object stores its values under property names. we've already made use of object properties many times over:

what we haven't done is create them from scratch yet.

constructing objects

like other core datatypes in actionscript, objects can be constructed either literally:
var obj:Object = { myProperty : myValue };
or via a constructor function:
var obj:Object = new Object();
obj.myProperty = myValue;
property values can any other value type, whether native: or not:

accessing object properties

when you know the name of an object property, you can access it directly, using the dot syntax— obj.propertyName :
trace(Math.PI);
var clark:Object = { weakness : "kryptonite" };
trace(clark.weakness);
or alternatively, using an associative array syntax— obj["propertyName"] :
trace(Math["PI"]);
var clark:Object = { weakness : "kryptonite" };
trace(clark["weakness"]);
when you don't know all the property names of an object, or need to look at them all, you can use a for .. in loop:
var obj:Object = { a : 1,   b : "two",   c : [3,2,1],   d: false };
for (propertyName:String in obj) {
    var val = obj[propertyName];
    trace(propertyName +" : " +val);
}
the loop looks at each exposed property in the object, and assigns the property name to the iterator variable you declare. then you can access the value assigned to the property using the associative array syntax. for custom-built objects, all properties will be exposed, but for built-in objects like MovieClip, not all properties are exposed to a for .. in loop.

 

organization

programming is all about organization. let's review all of the data structures we've covered so far and see how they enable us to organize, by packaging and labeling.

variables

variables let us label values, and then use the label in place of the value.

arrays

arrays allow us to gather multiple values into a single variable that stores the values in order.

functions

functions allow us to gather multiple statements into a single variable and then execute the statements from multiple places without rewriting them.

objects

objects allow us to collect multiple values into a single variable that stores the values by name.

movie clips

movie clips allow us to collect graphics and code into an environment that has a concept of time.

object oriented programming

hopefully you've seen by now that using objects can be very convenient. we don't need to worry much about how things work, just what controls are provided. complexity is encapsulated within the object, and exposed through a simpler API of properties and methods with explicit datatypes and method signatures.

just as variables become handy as soon as we start using the same value in multiple places, and functions become advantageous as soon as we have a few statements that we'd like to be able to reuse, objects are useful for grouping a set of conceptually related properties and methods, and controlling how they can be accessed and / or modified.

breaking a programming task into a structure of cooperating objects is called object-oriented programming. it is a methodology of organization and little more. but organization is a highly effective tool for addressing complexity. some programming tasks are trivial enough that no extra organization is needed. a simple timeline narrative may only need a few gotoAndPlay() function calls. but bigger projects generally benefit from an object-oriented technique in the following ways:

by being able to define our own objects, we can implement new datatypes to complement and expand on the native types offered by actionscript, while still being able to take advantage of compile-time type checking. we can build complex applications, but if we engineer them from simple, well-defined parts, those parts can be recombined in other ways to build other applications with less effort.