Follow Flex After Dark on Twitter

Get and Set Functions

ActionScript has support for special get and set functions that can encapsulate property access behind function calls, but without changing the property access syntax.

Before further explanation, let's see an example of a property exposed via get/set functions:

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

Note the extra get and set keywords in the function declarations. This tells ActionScript that the functions are for property access. While the code looks quite different than just using a public property, the usage is exactly the same.

The above code is analogous to the below code:

public class Person
{
   public var name:String;
}

Both examples of code, a public property and get/set functions, can be used as followed:

   person.name = "John";
   var example:String = person.name; // returns "John"

The key point of get and set functions is that you can start with public properties and later move to get/set functions, if necessary, without changing how the class is used.

Another example, this time making a read-only property (just a get function):

   public function get isReadOnly():Boolean
   {
      return true;
   }

Usage of a read-only property:

   if( object.isReadyOnly ) // calls the get isReadyOnly() function
   {
      // do something
   }
   
   // will not work
   object.isReadOnly = false;

Related Links:

Related Docs
  • ActionScript Functions ActionScript Functions are first-class objects and can be registered as event listeners.
Recent Docs
  • ActionScript Get/Set Functions ActionScript Get/Set Functions are special function handlers for property accessors.
  • Flex Binding Flex Data Binding enables objects and their values to be bound together so that when a source changes a target automatically gets updated.
  • Flex Modules Flex Modules are compiled to dynamically-loadable SWF files that can be loaded and unloaded at run-time.
  • Flex Progress Bar The Flex Progress Bar control is used for displaying progress to users.
  • ActionScript Language Introduction to the ActionScript 3.0 programming language.
Random Docs
  • Flex Builder Searching Searching files in Flex Builder.
  • Flex Images and Icons Overview of using images and icons in Flex projects.
  • Flex StyleSheets StyleSheets define styles for Flex components and can be defined inline or loaded at run-time.
  • Flash Cross Domain Policy A cross-domain policy file is an XML document that grants Flash Player permission to handle data across multiple domains.
  • ActionScript ExternalInterface ActionScript's ExternalInterface class and IExternalInterface interface allow for two-way communication between ActionScript and browser JavaScript.