AS1 OOP: Object Basics
by senocular
Objects, Objects Everywhere
In actuality, everything in Flash Actionscript is an object.
Not only are generic objects, movieclips and other obvious
objects (such as the XML object), but also buttons,
textfields, arrays, strings, numbers and even functions.
Each one is considered an object in Actionscript in some way
shape or form. This is an important bit of information to
understand when working with Flash Actionscript. The fact
that everything you define is an object will help you better
work with your variables and use them the best way you can.
You already saw how movieclips are objects. Think of an
array. We can take any array and also add our own custom
values to it as well.
- myArray = [];
- myArray.home = "Alaska";
- trace(myArray.home); // traces "Alaska"
Now there are some exceptions that kind of break the
definition of what an object is – at least as you know it –
though still technically qualify as an object. These are
certain definitions of numbers, strings and boolean (true or
false) values. You might have already figured this out or
have known about these differences. Numbers, strings and
true/false variables defined directly with their value are
1) variables not defined as an object reference and 2) not
container object variables capable of holding values other
than their own. For example, consider a variable defined to
be 5. When using a direct assignment of that value to the
number 5, it then is a variable not able to hold others.
- myNumber = 5;
- myNumber.home = "Atlantis";
- trace(myNumber.home); // traces undefined as it does
not exist
The same applies to strings defined directly with a
quoted string and booleans set directly to true or false.
- myString = "Hello";
- myString.home = "Angkor";
- trace(myString.home); // traces undefined as it does
not exist
- myBoolean = true;
- myBoolean.home = "Barakus";
- trace(myBoolean.home); // traces undefined as it does
not exist
Also, if you assign two separate variables to have the
same value, they will in fact be equivalent unlike with
objects as they are references
- num1 = 5;
- num2 = 5;
- trace(num1 == num2); // traces true
Assigning values in this manner for numbers, strings and
booleans can be considered the shorthand method, much in the
way curly braces ({}) are used for a shorthand method of
generic objects. Only here, the shorthand method used for
defining numbers, strings and booleans make them less
object-like. These variable structures also have an long
form (or object form) of definition, very similar to the
definition of other objects, like so
- myNumber = new Number(5);
- myString = new String("Hello");
- myBoolean = new Boolean(true);
For these variables now, each exist as variable objects
capable of having other variable values contained within
them. They still have their same base value, they’re just
now capable of containing other values as well – as you
would expect from objects.
- myNumber = new Number(5);
- myString = new String("Hello");
- myBoolean = new Boolean(true);
- myNumber.home = "Atlanta";
- myString.home = "Anchorage";
- myBoolean.home = "Baltimore";
- trace(myNumber.home); // traces "Atlanta"
- trace(myBoolean.home); // traces "Anchorage"
- trace(myString.home); // traces "Baltimore"
This use of the long syntax in object definition is just
like declaring generic object Objects with a base value. Any
kind of variable you make in Flash (aside from functions)
can be defined this way. The truth is, when you define an
generic object in that manner, using a base value, it is
actually converting the new Object call into a new Number or
a new String call based on what you passed in for the base
value. A generic object technically can’t have a base value
and still be an object Object. That base value means it has
to be some other object such as a number, string etc. which
is representative of that type of value.
So what makes basic numbers, strings and booleans
objects? Nothing about them seems to show that they are an
object. How can they be objects? Well, simply speaking, an
object in Flash is just the concept of understanding for
Flash when it comes to a variable type. Objects have
inherent in their being, more than just a base value. A
simple number is actually more than a simple number. There’s
other hidden functionality as every number is in its own way
an object. This can be seen better in strings.
Think about a string and what you can do with it in
ActionScript. Actionscript gives you all kinds of useful
commands for controlling and manipulating strings. For
example, you have the ability to convert a string to be all
uppercase using the toUpperCase method.
- title = "capitalize";
- trace(title.toUpperCase()); // traces CAPITALIZE
Notice how the toUpperCase is used on the title variable
using dot syntax as if it were included in the string as an
object. In fact, in a way, it is! As a string, it inherently
has access to all methods associated with strings – this
even if the string was not declared in an object manner with
new Object() or new String(). With this, you can see where a
non object variable demonstrates object behavior.