Introduction
Objects
Instances
Local
Inheritance
Prototypes
Override
Overwrite
Protection
Arguments
Constructors
Methods
Proto
Arguments Array
Callee
New
Extend
Super
Glossary
 
 
Debreuil Digital Works © 2001
 

Instances - and a brief description of 'New'

 

Definitions
Class: A template (used to set properties in an object).
Instance: An object (created using a Class).

To tie classes and instances together with what we've learned up to this point:

  • instances (objects) are real things that can have properties (like inst1.x has the property x).
  • instances can be thought of as containers, classes can be thought of as descriptions of things that will go in a container.
  • instances have a 'scope' they can't (naturally) see out side of... We will see a similar thing applies to classes.
  • instances are created by classes, and can be related to each other - we have just begun to touch on this topic...

We created a few new objects in the previous section, and though things worked like we probably expected them to, do we really understand what is happening? Let's take a medium close look. Here is a condensed version of the previous code (numbered only for convenience):

1) Template = function()
2) {
3)   this.x = 5;
4)   this.y = 7;
5) }
6) inst1 = new Template( );

How does inst1 end up with the properties x and y? How does actionscript know this is what we want? Let's go through it step by step. First the class is defined. That is just a definition; it has little to do until it is called. The first line to do something (other than setup) is line 6. The right side of an equal sign is always evaluated first, so we'll start there. The word new does many things, but most importantly it creates a new object (box, container...) that has no properties and no name. It then sends this box to whatever class the statement beside it points to, which in this case is the class called Template. Recap: the right side of the equation in line 6 has now sent 'a box with no name' to 'Template'.

Every class expects and receives an empty unnamed box when it is called. This box is given the temporary name "this" when the code block begins, and then its 'dragged' through the class. As it moves through, properties are assigned to it as per the class's instructions, using the "this" keyword. Think of a box going down a conveyer belt, being filled with different objects as it moves. Each step says, "this box gets one of these", "this box gets one of those"... In this example, the box is given two properties, x and y with the values 5 and 7. When the end of the class definition is reached, the box (that now has two new properties) is returned. So what line 6 now says is

inst1 = << a box(object) with two properties, x:5 and y:7 >>

So the last thing to do is slap the name on the box, which is 'inst1' in this case, and you are done. Imagine people waiting at the end of the conveyer belt, each writing his or her name on a box, and taking it home. It's now their box, whereas before it was just a box on a conveyer belt. The last step then is to give the object a name, which is what the left side of the equation in line 6 does once the class has finished running. You can check the resulting object for the two properties x and y; they should have the values 5 and 7.

The only thing that can define a class in actionscript is a function (version 5 anyway) - though it can be either your own function, or a built-in one like Color or Array. Also true, only classes (built-in or otherwise) can create instances. Shortly, we will divide functions into two types, classes and methods, and then just stop using the word 'function' in order to avoid confusion.

The word 'instance' is probably familiar to you from Flash. You create instances of symbols in the library by dragging them on to the stage. This is not a similar thing to creating an instance from a class - this is the *same* thing. Dragging a symbol on to the stage is creating a 'new' object, so an empty container is created (a blank movieclip if its a movieclip). This container is then passed to the class template, and 'dragged through it', picking up different properties along the way, again, like a conveyer belt. The class of course is the symbol definition you find in the library, and the properties are the graphical elements in the symbol (what is added to the box on the conveyer belt). You already understand why it is useful to use templates to create objects in Flash. Doing so in actionscript is equally useful for much the same reasons.

Perhaps you don't feel that every class receives an unnamed box. Fair enough, for example:

A = function()
{
    return 7;
}
y = A(); // 7

The first thing to say, is this not a class - classes never return values and they are always invoked using the new operator. This is actually a method, which we will cover shortly. Briefly, a method is a function that belongs to a class - in fact the terms function and method are often used interchangeably.Classes, however, are very different. Whatever we call them, all functions do receive an object, and they set the value "this" to that object. Classes receive blank ones, and methods receive existing ones. But back to the point...

By only saying A( ), it certainly seems that you are in no way sending an object into the function to stick properties on - where would it be coming from? However if you don't explicitly send an object (like when you use new, or inst.A( ); ), the current 'container' object is implied and sent. The container that all of this code is taking place in is of course _root (which is _level0 if that's where the code is running from), so that is indeed what is sent ( A( ) really means _root.A( ); ). The following code shows this:

A = function()
{
this.x = 5; // _root.x = 5
return 7;
}
var y = A();
trace (_root.x); // 5
trace (y);       // 7

As you can see, _root was sent into the function, and assigned to the "this" keyword. When the function was run, the properties assigned to "this" were being assigned to _root, as the trace shows.

Classes < < Home > > Local