12
Jun
08

ActionScript Modifiers Summary page and ActionScript 4.0 wishlist

Let’s talk about some basic Object Oriented programming (OOP) principles - modifiers. Although some modifiers are used often, especially access modifier (private, public, protected and internal), others are rarely used since they are not needed in most cases. With that said, we tend to forget them and end up never using them, even when they are needed.

I decided to create a useful one page summary that can be revisited every time you forget the different types of modifiers available in ActionScript 3.0.

To begin let’s split the modifiers into two groups;

  • Access modifier – is a modifier that changes the visibility of a class or its members.
  • AS 3.0 Modifiers - used to modify declarations of types and type members in AS.


Access Modifier AS 3.0 Modifier
public dynamic
private Final
protected native
internal override
static
interface
abstract *

Access Modifiers

Modifier Used on Meaning
private var
const
function
namespace
Available to the object that declares it.
protected var
const
function
namespace
Available to object that declares it and subclass.
public var
const
function
namespace
Accessible to any object.
internal var
const
function
namespace
Available to any object within the same package.

AS 3.0 Modifiers

Modifier Used on Meaning
dynamic class Properties can be added at runtime.
final class
function
Cannot be subclassed.
Cannot be overridden dynamically looked up.
native class
function
Flash Player native code.
Only signature.
override function Replaces an inherited method.
static var
const
function
Belongs to the class, not to instances of the class.
interface class Define a set of methods but not their implementation.
Abstract * class Class includes method with no implementation. To be overridden later by a subclass.

* Keyword is not available

As default I highly recommend you to declare your objects as private unless you need to let other class access the object. The goal is to keep your object as abstracted as possible; making your code as loosely coupled to other objects, which will increase the re-usability of the objects.

When developing API’s in Action Script we design and choose from different modifiers, however when the wrong modifier is used the object can be misused and the objects become coupled to other objects.

Here’s my wish list for AS 4.0;

  • Action Script supports the abstract modifier.
  • Action Script support for static classes to insure they are not being created by creating an instance reference.
  • Action Script allow the creation of Private class constructors.

There are many scripts out there to add these modifiers but many seem like a hack than an elegant solution. They basically help you enforce how the modifiers should act.
The only one that I see as a good solution, and not as somewhat of a hack is for creating Abstract methods and classes, here’s an example of how to create and implement;


package com.utils
{
    import flash.errors.IllegalOperationError;
    public class AbstractClass
    {
		public function AbstractClass(self:MyAbstractClass)
    	{
    		if(self != this)
    		{
    			throw new IllegalOperationError(”Abstract class did not receive reference to self. MyAbstractClass cannot be instantiated directly.”);
    		}
    	}

		public function method(param:String):void
    	{
    		throw new IllegalOperationError(”abstract function must be overriden”);
    	}
    }
}

And here’s how you would implement the class;


package com
{
    public class MyImplementClass extends AbstractClass
    {
	    public function MyImplementClass()
    	{
    		super(this);
    	}

		public override function method():void
    	{
    		trace("Method");
    	}
    }
}

4 Responses to “ActionScript Modifiers Summary page and ActionScript 4.0 wishlist”


  1. 1 Xegnma Feb 15th, 2009 at 2:41 pm

    I second your vote for Private Class Constructors.

  2. 2 elad.ny Feb 16th, 2009 at 9:02 pm

    Actionscript 4 will not be ECMAScript compatible, and will allow Adobe to get “out of the box” and take the language to the next level. I believe, we will see improvements in modifiers.

  3. 3 Chris Luebcke Mar 6th, 2009 at 3:06 am

    Thanks for the good summary. I wish I could add method overloading to the wish list, but that was torpedoed by the adoption of the …rest argument. You can’t disambiguate

    function a() : void;
    function b(x : int) : void;
    function c(x : int, y : int) : void;

    If you might also have

    function d(…rest) : void;

    Oh well, Sometimes sending a configured Object is more useful, anyway.

    elad.ny: ActionScript 3 is not ECMAScript compatible, either, so we’re not losing much there. Plus it sounds like the working group pretty much self-destructed, for which Adobe can be held only partly to blame. This is why these organizations tend to go a full decade before major releases.

  4. 4 elad.ny Mar 7th, 2009 at 8:57 am

    Good point Chris, method overloading would be a great addition to AS. Regarding ECMAScript, according to Wikipedia AS3/Flex is based on ECMA-262, edition 3 (see: http://en.wikipedia.org/wiki/ECMAScript).