EladElrom.com

Deep Dive Into Technology

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");
    	}
    }
}

Remove border from TextInput or DateField component

If you ever tried to remove the border from text input components such as TextInput or DateField you will find that whatever you do you cannot remove a line that look like a border from your top border. Anything you try such as:

dropShadowColor : “0xFFFFFF”;
dropShadowEnabled : “false”;
border-thickness-bottom : 0;
border-thickness-top : 0;
border-thickness-left : 0;
border-thickness-right : 0;

Or directly on the component such as;

borderColor=”white” borderThickness=”0″

You just can’t get the fu#$%$ borders off! The reason is that flex assign a border skin: borderSkin=”mx.skins.halo.HaloBorder” which will always going to leave a line on the top corner.

The solution is actually simple; either use
BorderSkin: Classrefernece(null); in CSS

Or create a skin and don’t draw anything leaving the skin borders empty, that will remove the borders, shadows etc;

package com.skins
{
import mx.skins.Border;

public class TextInputBorderlessSkin extends Border
{
override protected function updateDisplayList(w:Number, h:Number):void
{
super.updateDisplayList(w, h);
}
}
}

Now you can assign the skin in your CSS to your component text input;

.BorderlessTextInput {
    border-skin: ClassReference("com.skins.TextInputBorderlessSkin");
}

Play around with skins in Flex, you’ll have a lot of fun!