Wednesday, March 25, 2009

G is for Get/Set Functions

In a lot of programming languages we're use to seeing "getter and setter" functions "wrapped" around private properties. The idea is to encapsulate the access of variables behind these "accessor" functions.

You won't see nearly as many getter/setter functions in ActionScript. It's not because of laziness or "bad OOP". It is because ActionScript has support for special get and set functions that can encapsulate property access behind function calls, but without changing the property access syntax.

public class Person
{
private var _name:String;

public function get name():String
{
return this.name;
}

public function set name( value:String ):void
{
this.name = value;
}
}
Learn more about ActionScript Get/Set Functions at Flex After Dark...