OOP4AS2#2: Class members

In installment 1, we learned how to define a class in AS2, and how to make instances of that class – if you didn’t read installment 1, you should. Now, we’re going to have a look at how to add members (properties and methods) to our class. We’ll also have a quick look at using strict-typing.

Read on…

Throughout this, it’s important to note that AS2 IS case sensitive, so mind your P’s and q’s.

Adding members to a class is super easy – just define variables and functions inside a class block, and they become properties and methods of that class. Those properties and methods will then be available to every instance of the class. Let’s take a look:

class GThing {
var myName = "gMan";
function getGreeting(p_salutation) {
return p_salutation+" "+myName;

A couple of important things to note in the above example:

  1. You need to prefix property definitions with var.
  2. Unlike AS1 where you had to prefix property names with “this” in methods, you can now reference them directly. The default scope for methods in AS2 classes is the instance scope (ie. “this” is implied).

Now if we jump back to the FLA, and test this out, you’ll (hopefully) see it all works:

myG = new GThing();
// traces "Cheers gMan"

In AS2, you also have the ability to use compile-time strict-typing. Basically that means that you can ask the Flash compiler to alert you if you try to squeeze the wrong type of Object (say a Number) into the wrong type of variable (a String for instance).

To do this for properties, variables and parameters you simply tack a colon, followed by the object name after the variable name when you declare it. Defining a return type for a function is similar, just tack the colon and object name after the parameter parenthesis of the function.

Let’s see what this looks like with our example above:

class GThing {
// myName is typed to String:
var myName:String = "gMan";
// getGreeting returns a String
// the parameter p_salutation is a String
function getGreeting(p_salutation:String):String {
return p_salutation+" "+myName;

Then in the FLA:

// myG is a GThing
var myG:GThing = new GThing();
// traces "Cheers gMan"
// We could cause a compiler error with:
var myG:GThing = new GThing();
// error, type mismatch - Boolean != String

An important note on the above example is that in order to use strict typing with variables you must declare the variable with “var”. You can see this in the first line of the FLA code.

Remember, the type checking is only compiler-time – at run-time an external SWF could still plug a String into your GThing typed variable (something about that sounds very wrong).

Stay tuned for more OOP goodness.

And don’t forget, this is just the tip of the iceberg… to get all the juicy details, you have to lay your greasy hands on the upcoming “Flash MX 2004 Demystified” from Macromedia Press.

Grant Skinner

The "g" in gskinner. Also the "skinner".



  1. Question relating with strict-typing:

    before I used somethink like this:

    function erer ()


    for () {

    if () return false;


    return myObject;


    Does such construction have sence?

  2. function getGreeting(p_salutation:String):String {


    Does the colon and datatype after the arguments parentheses indicate the function returns a string? That the function itself is a string?

  3. Serge – You can return undefined or null to end function execution without returning a value of the specified type. The easiest way to do this is by just using “return;” with no value.

    Mike – Yes, it indicates the function will return a value of type String.

  4. OOP para AS2 #2 : miembros de una clase

    Esta página es una traducción de un artículo publicado por Grant Skinner, publicado en este blog.

    En el capĂ­tulo 1, aprendimos como definir una clase en AS2, y como crear instancias de esa clase – si no has leĂ­do el capĂ­tulo 1, deberĂ­as. Ahora vamos a…

  5. This article is now available in german!

    Check it out at:


Comments are closed.